I am working on one WPF (MVVM) Application with Caliburn Micro, I went by the standard wordings that in properties should reside in VM, In My App I've 3 textboxs and one datagrid, What I want is that whenever use selects any row the Textboxs must show the selected values (Single Selection Only !), Textboxes will show different data based on Columns. I've model also for the same structure, The problem is I've set the x:name of the control as property as per naming conventions of CM, now the selection is taken from the model so How can I set the local VM property from the Model property.
My Model :
public class PackageModel
{
public int id { get; set; }
public string sessionName { get; set; }
public int sessionInMins { get; set; }
public int sessionAmount { get; set; }
public bool isActive { get; set; }
}
My ViewModel :
private string _packName;
public string PackageName
{
get { return _packName; }
set
{
_packName = value;
NotifyOfPropertyChange(() => PackageName);
}
}
private int _amount;
public int Amount
{
get { return _amount; }
set {
_amount = value;
NotifyOfPropertyChange(() => Amount);
}
}
private int _mins;
public int Mins
{
get { return _mins; }
set {
_mins = value;
NotifyOfPropertyChange(() => Mins);
}
}
private bool _isActive;
public bool IsPackageActive
{
get { return _isActive; }
set {
_isActive = value;
NotifyOfPropertyChange(() => IsPackageActive);
}
}
private List<PackageModel> _packageList;
public List<PackageModel> PackageList
{
get { return _packageList; }
set
{
_packageList = value;
NotifyOfPropertyChange(() => PackageList);
}
}
private PackageModel _selectedPackage;
public PackageModel SelectedPackage
{
get
{
return _selectedPackage;
}
set
{
_selectedPackage = value;
NotifyOfPropertyChange(() => SelectedPackage);
}
}
}
My View :
<!--ROW 1-->
<TextBox
x:Name="PackageName"
Width="210"
Margin="5 5"
Controls:TextBoxHelper.Watermark="Package Name"
Controls:TextBoxHelper.ClearTextButton="True"
HorizontalAlignment="Left"
Grid.Row="0"
Grid.Column="0"/>
<!--ROW 2-->
<StackPanel Grid.Row="1" Grid.Column="0" Orientation="Horizontal">
<TextBox
x:Name="Mins"
Width="100"
Margin="5 5"
Controls:TextBoxHelper.Watermark="Min(s)"
Controls:TextBoxHelper.ClearTextButton="True"/>
<TextBox Width="100"
x:Name="Amount"
Margin="5 5"
Controls:TextBoxHelper.Watermark="Amount"
Controls:TextBoxHelper.ClearTextButton="True"/>
<CheckBox
x:Name="IsPackageActive"
Margin="5 5"
Content="Is Active?"
IsChecked="{Binding SelectedPackage.isActive}"/>
</StackPanel>
<!--ROW 3-->
<DataGrid x:Name="PackageList"
SelectedItem="{Binding SelectedPackage, Mode=TwoWay}"
Grid.Row="2"
Grid.ColumnSpan="1"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserSortColumns="False"
CanUserReorderColumns="False"
CanUserResizeRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding id}" Visibility="Hidden" IsReadOnly="True"/>
<DataGridTextColumn Header="Name" Binding="{Binding sessionName}" Width="120" IsReadOnly="True"/>
<DataGridTextColumn Header="Min(s)" Binding="{Binding sessionInMins}" IsReadOnly="True"/>
<DataGridTextColumn Header="Amount" Binding="{Binding sessionAmount}" IsReadOnly="True"/>
<DataGridCheckBoxColumn IsReadOnly="True" Header="Is Active?" Binding="{Binding isActive}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
Please suggest if I am following any wrong standard or wrong way of setting properties.