tags:

views:

362

answers:

1

Is it possible to use a multiTrigger to evaluate properties on multiple elements? That don't reside within a template, but are within the Usercontrol/Window.

Example:

<CheckBox x:Name="checkBox1" />
<CheckBox x:Name="checkBox2" />

<CustomControl>
   <CustomControl.ContentTemplate>
              <DataTemplate>
                        <DataTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition
                                        SourceName="checkBox1"
                                        Property="IsChecked"
                                        Value="False" />
                                    <Condition
                                        SourceName="checkBox2"
                                        Property="IsChecked"
                                        Value="True" />
                                </MultiTrigger.Conditions>
                                <MultiTrigger.Setters>
                                    <Setter
                                        Property="Visibility"
                                        Value="Collapsed" />
                                </MultiTrigger.Setters>
                            </MultiTrigger>
                        </DataTemplate.Triggers>
                    </DataTemplate>
</CustomControl.ContentTemplate>

In a normal Trigger we can use the sourceName to trigger against properties on either of those checkbox controls, but with a multiTrigger I get build errors when it attempts to find those controls.

Note: The reason I'm using a DataTemplate as opposed to a Style is mentioned here

+1  A: 

You can indeed use source name, but your DataTemplate doesn't appear to be defined correctly. I've tried filling out your example which seems to work:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <Grid>
     <Grid.Resources>
      <DataTemplate DataType="{x:Type sys:String}">
       <StackPanel>
        <TextBlock x:Name="textBlock" Text="{Binding}"></TextBlock>
        <CheckBox x:Name="checkBox1" />
        <CheckBox x:Name="checkBox2" />
       </StackPanel>
       <DataTemplate.Triggers>
        <MultiTrigger>
         <MultiTrigger.Conditions>
          <Condition
           SourceName="checkBox1"
           Property="IsChecked"
           Value="False" />
          <Condition
           SourceName="checkBox2"
           Property="IsChecked"
           Value="True" />
         </MultiTrigger.Conditions>
         <MultiTrigger.Setters>
          <Setter
          TargetName="textBlock"
           Property="Visibility"
           Value="Collapsed" />
         </MultiTrigger.Setters>
        </MultiTrigger>
       </DataTemplate.Triggers>

      </DataTemplate>
     </Grid.Resources>
     <ContentControl>
      <sys:String>Foo</sys:String>
     </ContentControl>
    </Grid>
</Page>

The problem appears to be that you don't define your CheckBoxes inside your DataTemplate, which is where they need to be.

micahtan
Thank you for your answer, I understand your solution and it certainly works in this scenario. The solution I was shooting for was one that didn't require for this controls to reside within the same template. With a single Trigger I can use SourceName with controls that don't reside within a template. I was hoping for similar behaviour with MultiTriggers. I guess your answer is that this isn't possible.
Chris Nicol
I don't think it is possible since the name scoping for the DataTemplate Triggers is the DataTemplate. You may be able to write some MultiDataTriggers, but that would restrict which DataType you're using as well as require that you to provide those properties.
micahtan