WPF Fundamentals: How to Set External Property on a DataTrigger?
I am trying to get my head wrapped around a WPF model.
I have a list of items. String identifiers are specified inside the list items. This works great. I want it to be such that the ID from the currently selected item is available in my code for the closing control.
I have it:
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="320">
<Label Content="{Binding Path=ShortName}" Style="{StaticResource ListHeader}"/>
<TextBlock TextWrapping="Wrap" Text="{Binding Path=Description}" Style="{StaticResource ListText}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
And I think I should add something like:
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True">
<Setter Property="" TargetName="">
<Setter.Value>
</Setter.Value>
</Setter>
</DataTrigger>
But I'm at a loss as to how to set up an installer to set a property that is part of an embedded control (i.e. the outside world). I think I have it somehow?
a source to share
If you are trying to access a property on a selected list item from outside the list, you can do the following in your code:
CustomItem item = (CustomItem)listBox1.SelectedItem;
MessageBox.Show(item.ShortName);
And your xaml looks like this:
<ListBox Height="100" Name="listBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="stackPanel1">
<Label Content="{Binding Path=Shortname}"/>
<TextBlock Text="{Binding Path=Description}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
You just cast the selected item from the list to your object type and then access the object's property.
Hope this is what you were.
a source to share
Have you tried using the SelectedValuePath property ?
When you have a list of clients named ItemsSource and you set the SelectedValuePath to the name, the SelectedValue property will return the name of the Client instead of the client ...
In your code behind, SelectedValue will be Name, SelectedItem will return Customer object .. in my example ..
Hope this helps somehow ...
Good luck!
a source to share