为多个控件提供一个数据触发器

发布于 2024-12-13 03:38:04 字数 854 浏览 0 评论 0原文

我希望在 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>

I want to have one Style DataTrigger inside my window.resources that can be used for multiple expanders. The DataTrigger is bound to an enum value inside my ViewModel, and based on the enum value, I want the the correct expanders to be collapsed. For example: If the enum value is set to "A" then I want only the expander associated with type "A" to be visible, and the rest of the expanders to become collapsed.

I was thinking of something like this:

    <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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唔猫 2024-12-20 03:38:04

我相信您可以为每个具体 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}"/>

I believe you would be able manually setup Styles for each concrete Expander class, to avoid Trigger code duplication you can use Style.BasedOn to build a hierarchy of dependent styles:

 <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}"/>
夜声 2024-12-20 03:38:04

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

见下文(我在本例中使用画布):

   <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>

I was able to solve this (using multiple triggers) with a static resource that caused each Control to 'Collapse' and then having separate 'DataTriggers' inside each Control where the associated enum value causes the visibility to be 'Visible'.

See below (I used canvas in this example):

   <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文