为多个控件设置一个数据触发器

时间:2011-11-02 18:44:37

标签: .net wpf xaml datatrigger

我希望在window.resources中有一个Style DataTrigger,可以用于多个扩展器。 DataTrigger绑定到我的ViewModel中的枚举值,并且基于枚举值,我希望折叠正确的扩展器。例如:如果枚举值设置为“A”,那么我只希望显示与类型“A”关联的扩展器,并且其余的扩展器将被折叠。

我在考虑这样的事情:

    <Style TargetType="{x:Type Expander}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Type}" Value="A">
                // In here i would set the expander associated w/ "A" to Visible 
                // and have the rest of the expanders collapsed. Since TargetName is 
                // not allowed within a "Setter" property of a style, I am not sure on how to accomplish this.
            </DataTrigger>
            <DataTrigger Binding="{Binding Type}" Value="B">
               // Same concept as above
            </DataTrigger>
        </Style.Triggers>
    </Style>

2 个答案:

答案 0 :(得分:0)

我相信您可以为每个具体的Expander类手动设置样式,以避免触发器代码重复,您可以使用Style.BasedOn来构建依赖样式的层次结构:

 <Style x:key=ExpanderBaseStyle" TargetType="{x:Type Expander}">
   <!-- trigger logic -->
 </Style>

 <Style TargetType="{x:Type FirstExpander}" 
                     BasedOn="{StaticResource ExpanderBaseStyle}"/>

 <Style TargetType="{x:Type SecondExpander}" 
                     BasedOn="{StaticResource ExpanderBaseStyle}"/>

答案 1 :(得分:0)

我能够使用静态资源解决这个问题(使用多个触发器),导致每个控件“折叠”,然后在每个控件中都有单独的“DataTriggers”,其中关联的枚举值使得可见性为“可见”。

见下文(我在本例中使用了canvas):

   <Grid>
    <Grid.Resources>          
        <Style x:Key="CanvasStyle">
            <Setter Property="Canvas.Visibility" Value="Collapsed"/>
        </Style>
    </Grid.Resources>
    <ComboBox IsSynchronizedWithCurrentItem="True" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="comboBox1" VerticalAlignment="Top" Width="153" ItemsSource="{Binding IDs}" MaxDropDownHeight="75" SelectedValue="{Binding SelectedValue}"/>
    <StackPanel Height="128" HorizontalAlignment="Left" Orientation="Horizontal">
        <Canvas Height="100" Name="canvas1" Width="100" Background="#FFC70D0D">
            <Canvas.Style>
                <Style BasedOn="{StaticResource CanvasStyle}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Type}" Value="A">
                            <Setter Property="Canvas.Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Canvas.Style>
        </Canvas>
        <Canvas Background="#FF63C70D" Height="100" Name="canvas2" Width="100">
            <Canvas.Style>
                <Style BasedOn="{StaticResource CanvasStyle}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Type}" Value="B">
                            <Setter Property="Canvas.Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Canvas.Style>
        </Canvas>
        <Canvas Background="#FF0D55C7" Height="100" Name="canvas3" Width="100">
            <Canvas.Style>
                <Style BasedOn="{StaticResource CanvasStyle}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Type}" Value="C">
                            <Setter Property="Canvas.Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Canvas.Style>
        </Canvas>
    </StackPanel>
</Grid>