2011-05-17 89 views
3

我有一個場景,我需要根據單選按鈕被選中還是未選中來隱藏一些內容。出於某種原因,我無法按照我所期望的方式工作。這種行爲與我所期望的相反。如果我調整我的xaml以適應我看到的實際行爲,那麼一切都會隱藏起來。RadioButton上的DataTrigger IsChecked

基本上我所擁有的是兩個單選按鈕,標記爲Fixed和Cycle。選中固定後,我希望與固定相關的文本框具有可見的前景,並且與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> 

任何想法?

+0

原來問題出在鍵盤和用戶之間。顯然,團隊中的開發人員將我在文本框中使用的LightForeground樣式更改爲與我的背景相同的顏色,從而使它們具有透明外觀。感謝@ H-B和@ robert-rossney他們的建議。 – KyleLib 2011-05-18 15:34:33

回答

1

只是使文本透明是一個壞主意,用戶仍然可以編輯它,而它是透明的。除此之外,代碼有些模糊和冗餘,不要爲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> 
+0

感謝您的意見。我同意你關於隱藏文字的陳述。 FWIW,我也禁用了包含文本框的dockpanel,以防止用戶將注意力集中在它上面。要求是提供一個設置功能,用戶可以指定如何對數字樣本流進行平均。選項是**固定的**時間間隔,或者是以赫茲爲單位的**循環數**。還有其他內容與文本框一起出現,比如時間單位等,爲簡潔起見我省略了這些內容,這些內容僅在選中的複選框的上下文中有意義。 – KyleLib 2011-05-18 12:27:18

1

如果您'使用綁定來設置單選按鈕的值,不要使用組。如果您使用的是羣組,請勿使用綁定。兩人在一起打球不好。

這可能不是你所看到的原因。但我敢打賭。這肯定會干擾您診斷問題的能力,因爲在您開始點擊按鈕後,它們的綁定不再起作用。