Results 1 to 2 of 2

Thread: Change datatrigger binding programmatically

  1. #1

    Thread Starter
    Member
    Join Date
    May 2007
    Posts
    40

    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>

  2. #2
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    1. 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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width