wpf – RadioButton IsChecked上的DataTrigger

我有一个场景,我需要根据是选中还是取消选中单选按钮来隐藏一些内容.出于某种原因,我不能按照我期望的方式使用它.这种行为与我的期望相反.如果我调整我的xaml以适应我看到它的实际行为,一切都会被隐藏.

基本上我有两个标有固定和循环的单选按钮.当选中Fixed时,我希望与Fixed关联的文本框具有可见的前景,并且与Cycle关联的文本框具有透明的前景,反之亦然.我所看到的恰恰相反.

这是我的触发器:

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

有任何想法吗?

最佳答案 只是让文本透明是一个坏主意,用户仍然可以在透明的情况下编辑它.除了代码是混淆和冗余之外,不要为TextBox创建两个样式,两个样式都包含相同的setter,创建基本样式并使用BasedOn作为子样式.

不太确定你的代码出错的地方,我建议你清理一下,也许有一些逻辑错误,这里也是一个可以投入Kaxaml的工作示例:

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