-
Oct 8th, 2009, 10:06 AM
#1
Thread Starter
Member
Change datatrigger binding programmatically
I have a listbox which uses an itemtemplate and is bound to a datatable.
I am using datatriggers to do some of the formatting depending on the data in a particular column.
What I want to do is change the binding on the datatrigger programmatically, eg change it from binding to colOne to binding to colTwo.
Can anyone tell me how I can access the triggers and change the binding from code?
Code:
<ListBox DockPanel.Dock="Top" Name="ListBox1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Margin="2" x:Name="txtOne" Text="{Binding Path=ColOne}"></TextBlock>
<TextBlock Margin="2" x:Name="txtTwo" Text="{Binding Path=ColTwo}"></TextBlock>
<TextBlock Margin="2" x:Name="txtName" Text="{Binding Path=Name}"></TextBlock>
</StackPanel>
<DataTemplate.Triggers>
<DataTrigger x:Name="trigZero" Binding="{Binding Path=ColOne}" Value="0">
<Setter TargetName="txtName" Property="Background" Value="White"></Setter>
</DataTrigger>
<DataTrigger x:Name="trigOne" Binding="{Binding Path=ColOne}" Value="1">
<Setter TargetName="txtName" Property="Background" Value="Gray"></Setter>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
-
Oct 12th, 2009, 10:41 AM
#2
Re: Change datatrigger binding programmatically
I've never tried this before so maybe this isnt the best way but here is what I came up with:
To access the first trigger in your datatemplate:
vb Code:
Dim trigzero As DataTrigger = DirectCast(ListBox1.ItemTemplate.Triggers(0), DataTrigger)
Now you can access the Binding property of that object and I assume that is what you were after
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|