Where to Store ID Field In Silverlight
- by Blake Blackwell
I am trying to use the MVVM pattern, and I am wondering where I place an ID value. Do I create a TextBlock that has its visibility property set to collapsed? Or is there a better place to store this kind of data? Currently the command parameter is set to:
CommandParameter="{Binding ElementName=Name,Path=Text}"
But I'd rather it be something like:
CommandParameter="{Binding ElementName=Id,Path=Text}"
I'm totally up for suggestions on what the best way there is to do this, since I'm new to this pattern and presentation language.
<ListBox x:Name="MyListBox" ItemsSource="{Binding MyData, Mode=TwoWay}" BorderThickness="0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="Visible" IsChecked="{Binding Visible, Mode=TwoWay}" Command="{Binding ElementName=MyListBox, Path=DataContext.MyCommand}" CommandParameter="{Binding ElementName=Name,Path=Text}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
EDIT: Option 1
<CheckBox x:Name="Visible" IsChecked="{Binding Visible, Mode=TwoWay}" Command="{Binding ElementName=MyListBox, Path=DataContext.MyCommand}" CommandParameter="{Binding Id}" />
This option gives me just the Id, which is useful in many situations.
EDIT: Option 2
<CheckBox x:Name="Visible" IsChecked="{Binding Visible, Mode=TwoWay}" Command="{Binding ElementName=MyListBox, Path=DataContext.MyCommand}" CommandParameter="{Binding}" />
This option gives me the full model, which is even useful for my particular situation.