I've a MainView with MyControl in XAML. I need to write my control using MVVM, so I've MyControl.xaml that defines the layout with some binded properties, MyControl.xaml.cs, which stores only constructor and DependencyProperties (I need to be able to set them in Styles.xaml ResourceDictioanry), MainView.xaml (with main view UI) - the C# code associated with that MainView is left unchanged.
MyControl.xaml:
<UserControl x:Class="MainWindowModule.Views.MyControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:prism="http://prismlibrary.com/"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:viewmodels="clr-namespace:MainWindowModule.ViewModels"
d:DataContext="{d:DesignInstance Type=viewmodels:MyControlViewModel}"
prism:ViewModelLocator.AutoWireViewModel="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<prism:InvokeCommandAction Command="{Binding LoadedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<Grid>
<Rectangle x:Name="rect" Width="{Binding Width}" />
</Grid>
</UserControl>
MyControl.xaml.cs:
public partial class MyControl:UserControl {
public static readonly DependencyProperty NumberProperty =
DependencyProperty.Register("Number", typeof(int), typeof(MyControl), new PropertyMetadata(0));
public MyControl() {
InitializeComponent();
}
}
MyControlViewModel.cs:
public class MyControlViewModel:BindableBase {
public int Number {
get { return _number; }
set { SetProperty(ref _number, value); }
}
public int Width {
get { return _width; }
set { SetProperty(ref _width, value);}
}
public DelegateCommand LoadedCommand { get; set; }
private int _number = 0;
private int _width = 100;
public MyControlViewModel() {
LoadedCommand = new DelegateCommand(OnLoad);
}
private void OnLoad() {
//Load resources
}
}
MainView.xaml declaration of MyControl:
<Grid>
<local:MyControl x:Name="test" />
</Grid>
My problem is that I want to be able to specify Number property value in MainView.xaml, but I cannot see this property.