RadioButton 上的 DataTrigger IsChecked

发布于 2024-11-08 01:51:24 字数 3143 浏览 0 评论 0原文

我有一个场景,我需要根据单选按钮是否选中来隐藏一些内容。由于某种原因,我无法让它按照我期望的方式工作。该行为与我的预期相反。如果我调整我的 xaml 以适应我看到的实际行为,所有内容都会被隐藏。

本质上我有两个单选按钮,分别标记为“固定”和“循环”。选中“固定”后,我希望与“固定”关联的文本框具有可见的前景,与“循环”关联的文本框具有透明的前景,反之亦然。我所看到的恰恰相反。

这是我的触发器:

<Grid.Resources>
  <Style TargetType="TextBox" x:Key="FixedModeStyle">
    <Setter Property="Foreground" Value="Transparent" />
    <Setter Property="Width" Value="40" />
    <Setter Property="Height" Value="20" />
    <Setter Property="Margin" Value="10" />
    <Style.Triggers>
      <DataTrigger Binding="{Binding IsChecked, 
                             ElementName=rbtFixedMode}" Value="True" >
        <Setter Property="Foreground" 
                Value="{DynamicResource My.Fonts.Global.LightForeground}" />
      </DataTrigger>
      </Style.Triggers>                  
  </Style>
  <Style TargetType="TextBox" x:Key="CycleModeStyle">
    <Setter Property="Foreground" Value="Transparent" />
    <Setter Property="Width" Value="40" />
    <Setter Property="Height" Value="20" />
    <Setter Property="Margin" Value="10" />
    <Style.Triggers>
      <DataTrigger Binding="{Binding IsChecked, 
                   ElementName=rbtCycleMode}" Value="True" >
        <Setter Property="Foreground" 
                Value="{DynamicResource My.Fonts.Global.LightForeground}" />
      </DataTrigger>
    </Style.Triggers>
  </Style>
</Grid.Resources>

这是我的单选按钮和关联的文本框:

<RadioButton x:Name="rbtFixedMode" Content="Fixed" 
             GroupName="AveragingMode"
             Foreground="{DynamicResource My.Fonts.Global.LightForeground}" 
             IsChecked="{Binding AveragingWindowMode, 
                  Converter={StaticResource EnumToBooleanConverter}, 
                  ConverterParameter={x:Static  Processors:AveragingMode.Fixed}}" />
<DockPanel Grid.Row="1" IsEnabled="{Binding IsChecked, ElementName=rbtFixedMode}">
  <TextBox x:Name="txtFixedIntervalLength" 
           Style="{StaticResource FixedModeStyle}" DockPanel.Dock="Left" 
           Text="{Binding AveragingWindowFixedLength}" />
</DockPanel>

<RadioButton x:Name="rbtCycleMode" Content="Cycle" 
             GroupName="AveragingMode" Grid.Row="2"
             Foreground="{DynamicResource My.Fonts.Global.LightForeground}"                           
             IsChecked="{Binding AveragingWindowMode, 
                  Converter={StaticResource EnumToBooleanConverter}, 
                  ConverterParameter={x:Static Processors:AveragingMode.Cycles}}" />

<DockPanel Grid.Row="3" IsEnabled="{Binding IsChecked, ElementName=rbtCycleMode}">
  <TextBox x:Name="txtCycleIntervalLength" 
           Style="{StaticResource CycleModeStyle}" DockPanel.Dock="Left"  
           Text="{Binding AveragingWindowCycleLength}"/>
  <TextBlock x:Name="txbCycles" Text="Cycles" Margin="4,10"
             Foreground="{DynamicResource My.Fonts.Global.LightForeground}" />
</DockPanel>

有什么想法吗?

I have a scenario where I need to hide some content based on whether a radio button is checked or unchecked. For some reason I can't get this to work the way I expect it to. The behavior is the opposite of what I expect. If I adjust my xaml to accomodate the actual behavior that I'm seeing it everything gets hidden.

Essentially what I have is two radio buttons labeled Fixed and Cycle. When Fixed is checked I want the textbox associated with Fixed to have a visible foreground and the textbox associated with Cycle to have a transparent foreground and vice-versa. What I'm seeing is the exact opposite.

Here's my trigger:

<Grid.Resources>
  <Style TargetType="TextBox" x:Key="FixedModeStyle">
    <Setter Property="Foreground" Value="Transparent" />
    <Setter Property="Width" Value="40" />
    <Setter Property="Height" Value="20" />
    <Setter Property="Margin" Value="10" />
    <Style.Triggers>
      <DataTrigger Binding="{Binding IsChecked, 
                             ElementName=rbtFixedMode}" Value="True" >
        <Setter Property="Foreground" 
                Value="{DynamicResource My.Fonts.Global.LightForeground}" />
      </DataTrigger>
      </Style.Triggers>                  
  </Style>
  <Style TargetType="TextBox" x:Key="CycleModeStyle">
    <Setter Property="Foreground" Value="Transparent" />
    <Setter Property="Width" Value="40" />
    <Setter Property="Height" Value="20" />
    <Setter Property="Margin" Value="10" />
    <Style.Triggers>
      <DataTrigger Binding="{Binding IsChecked, 
                   ElementName=rbtCycleMode}" Value="True" >
        <Setter Property="Foreground" 
                Value="{DynamicResource My.Fonts.Global.LightForeground}" />
      </DataTrigger>
    </Style.Triggers>
  </Style>
</Grid.Resources>

Here's my radio buttons and associated textboxes:

<RadioButton x:Name="rbtFixedMode" Content="Fixed" 
             GroupName="AveragingMode"
             Foreground="{DynamicResource My.Fonts.Global.LightForeground}" 
             IsChecked="{Binding AveragingWindowMode, 
                  Converter={StaticResource EnumToBooleanConverter}, 
                  ConverterParameter={x:Static  Processors:AveragingMode.Fixed}}" />
<DockPanel Grid.Row="1" IsEnabled="{Binding IsChecked, ElementName=rbtFixedMode}">
  <TextBox x:Name="txtFixedIntervalLength" 
           Style="{StaticResource FixedModeStyle}" DockPanel.Dock="Left" 
           Text="{Binding AveragingWindowFixedLength}" />
</DockPanel>

<RadioButton x:Name="rbtCycleMode" Content="Cycle" 
             GroupName="AveragingMode" Grid.Row="2"
             Foreground="{DynamicResource My.Fonts.Global.LightForeground}"                           
             IsChecked="{Binding AveragingWindowMode, 
                  Converter={StaticResource EnumToBooleanConverter}, 
                  ConverterParameter={x:Static Processors:AveragingMode.Cycles}}" />

<DockPanel Grid.Row="3" IsEnabled="{Binding IsChecked, ElementName=rbtCycleMode}">
  <TextBox x:Name="txtCycleIntervalLength" 
           Style="{StaticResource CycleModeStyle}" DockPanel.Dock="Left"  
           Text="{Binding AveragingWindowCycleLength}"/>
  <TextBlock x:Name="txbCycles" Text="Cycles" Margin="4,10"
             Foreground="{DynamicResource My.Fonts.Global.LightForeground}" />
</DockPanel>

Any ideas?

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

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

发布评论

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

评论(2

断念 2024-11-15 01:51:24

仅仅使文本透明是一个坏主意,用户仍然可以在透明时对其进行编辑。除此之外,代码是混乱且冗余的。不要为 TextBox 创建两种样式,它们都包含相同的 setter;创建一个基本样式并使用 BasedOn 作为子样式。

不太确定你的代码哪里出了问题,我建议你清理它,也许存在一些逻辑错误,这里还有一个工作示例:

<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">
  <Page.Resources>
  
  </Page.Resources>
  <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
      <Grid>
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
      </Grid.ColumnDefinitions>
        <RadioButton Name="rbCycle" GroupName="g1" Content="Cycle"/>
        <RadioButton Name="rbFixed" GroupName="g1" Content="Fixed" Grid.Column="1"/>
        <TextBox Grid.Row="1" Text="cycle box">
            <TextBox.Style>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="Visibility" Value="Hidden"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsChecked, ElementName=rbCycle}" Value="True">
                            <Setter Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>
        <TextBox Grid.Row="1" Grid.Column="1" Text="fixed box">
            <TextBox.Style>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="Visibility" Value="Hidden"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsChecked, ElementName=rbFixed}" Value="True">
                            <Setter Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>
      </Grid>
  </ScrollViewer>
</Page>

Just making the text transparent is a bad idea, the user will still be able to edit it while it is transparent. Besides that the code is obfuscated and redundant. Do not create two styles for a TextBox, both containing the same setters; create a base-style and use BasedOn for sub-styles.

Not quite sure where your code goes wrong, i would suggest you clean it up, maybe there is some logical error, also here is a working example:

<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">
  <Page.Resources>
  
  </Page.Resources>
  <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
      <Grid>
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
      </Grid.RowDefinitions>
      <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="Auto"/>
      </Grid.ColumnDefinitions>
        <RadioButton Name="rbCycle" GroupName="g1" Content="Cycle"/>
        <RadioButton Name="rbFixed" GroupName="g1" Content="Fixed" Grid.Column="1"/>
        <TextBox Grid.Row="1" Text="cycle box">
            <TextBox.Style>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="Visibility" Value="Hidden"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsChecked, ElementName=rbCycle}" Value="True">
                            <Setter Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>
        <TextBox Grid.Row="1" Grid.Column="1" Text="fixed box">
            <TextBox.Style>
                <Style TargetType="{x:Type TextBox}">
                    <Setter Property="Visibility" Value="Hidden"/>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsChecked, ElementName=rbFixed}" Value="True">
                            <Setter Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>
      </Grid>
  </ScrollViewer>
</Page>
黑寡妇 2024-11-15 01:51:24

如果您使用绑定来设置单选按钮的值,请不要使用组。如果您使用组,请勿使用绑定。两个人在一起玩得不好。

这可能不是您所看到的原因。但我敢打赌是的。这肯定会干扰您诊断问题的能力,因为在您开始单击按钮后,它们的绑定不再起作用。

If you're using binding to set the values of your radio buttons, don't use groups. If you're using groups, don't use binding. The two don't play well together.

That may not be the cause of what you're seeing. But I bet it is. And it's certainly interfering with your ability to diagnose the problem, because after you start clicking on buttons their binding doesn't work anymore.

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