2014-04-02 31 views
3

我的問題是我有一組RadioButton,我想禁用另一個控件,具體取決於選擇哪個RadioButton。在XAML中使用RadioButtons啓用/禁用WPF控件

如果選擇幾個 RadioButton,我可以禁用該控件,這一點很重要。

E.g.

[] Enabled one 
[] Enabled two 
[] Disabled one 
[] Disabled two 

如果選擇了最後兩個按鈕之一,則應該禁用該控件。 。

注意

我打算回答這個問題我自己(Which should be ok according to this link)但如果他們解決了一個更好的辦法的問題,我會接受其他答案

回答

4

如果這是您需要它的唯一地方,那麼可以使用您的簡單解決方案,但我認爲在實際應用中,您可能會多次面對這種情況,所以我更喜歡使用通用解決方案來製作東西簡單。 對於我來說,我會用多轉換到使用邏輯OR從其他幾個布爾值計算結果布爾值,像這樣:

<Window.Resources> 
    <converters:MultiBoolOrConverter x:Key="MultiBoolOrConverter" /> 
</Window.Resources> 
<StackPanel> 
    <RadioButton GroupName="group" x:Name="enabledOne">Enabled one</RadioButton> 
    <RadioButton GroupName="group" x:Name="enabledTwo">Enabled two</RadioButton> 
    <RadioButton GroupName="group" x:Name="disabledOne">Disabled one</RadioButton> 
    <RadioButton GroupName="group" x:Name="disabledTwo">Disabled two</RadioButton> 

<TextBox Text="Test"> 
    <TextBox.IsEnabled> 
     <MultiBinding Converter="{StaticResource MultiBoolOrConverter}"> 
      <Binding Path="IsChecked" ElementName="enabledOne" /> 
      <Binding Path="IsChecked" ElementName="enabledTwo" /> 
     </MultiBinding> 
    </TextBox.IsEnabled> 
</TextBox> 

</StackPanel> 

和使用MultiBoolOrConverter.cs轉換器類:

using System; 
using System.Linq; 
using System.Globalization; 
using System.Windows.Data; 

namespace StackOverFlowTest.Converters 
{ 
    class MultiBoolOrConverter : IMultiValueConverter 
    { 
     public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
     { 
      return values.Cast<bool>().Any(value => value); 
     } 

     public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 

你還可能面臨您需要使用AND而不是OR,這裏是MultiBoolAndConverter轉換器:

using System; 
using System.Linq; 
using System.Globalization; 
using System.Windows.Data; 

namespace StackOverFlowTest.Converters 
{ 
    class MultiBoolAndConverter : IMultiValueConverter 
    { 
     public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
     { 
      return values.Cast<bool>().All(value => value); 
     } 

     public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 
    } 
} 

我希望這會幫助你。

UPDATE ---------------------

在我以前的解決方案,我用的邏輯:

The control should be enabled if one of the first two buttons are selected.

而不是你有什麼要求字面上:

The control should be disabled if one of the last two buttons are selected.

據工作過程中,但如果你想它完全按照你描述的那麼代碼需要變化不大使用轉換否定IsChecked布爾值:

<Window.Resources> 
<converters:MultiBoolAndConverter x:Key="MultiBoolAndConverter" /> 
<converters:BoolNegationConverter x:Key="BoolNegationConverter" /> 
</Window.Resources> 
<StackPanel> 
    <RadioButton GroupName="group" x:Name="enabledOne">Enabled one</RadioButton> 
    <RadioButton GroupName="group" x:Name="enabledTwo">Enabled two</RadioButton> 
    <RadioButton GroupName="group" x:Name="disabledOne">Disabled one</RadioButton> 
    <RadioButton GroupName="group" x:Name="disabledTwo">Disabled two</RadioButton> 

<TextBox Text="Test"> 
    <TextBox.IsEnabled> 
     <MultiBinding Converter="{StaticResource MultiBoolAndConverter}"> 
      <Binding Path="IsChecked" ElementName="disabledOne" Converter="{StaticResource BoolNegationConverter}" /> 
      <Binding Path="IsChecked" ElementName="disabledTwo" Converter="{StaticResource BoolNegationConverter}" /> 
     </MultiBinding> 
    </TextBox.IsEnabled> 
</TextBox> 

</StackPanel> 

BoolNegationConverter.cs

using System; 
using System.Globalization; 
using System.Windows.Data; 

namespace StackOverFlowTest.Converters 
{ 
    class BoolNegationConverter: IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      return !(value is bool && (bool)value); 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      return !(value is bool && (bool)value); 
     } 
    } 
} 
+2

這將是我的首選解決方案好。使用多值轉換器可以將這種行爲擴展到無限數量的布爾值 – Patrick

+0

這實際上比我的更簡潔。正如你所說在更多情況下它是可用的。感謝您的輸入! – FishySwede

+0

不客氣,我很高興它對你有用。 – Amer

2

我想出瞭解決的辦法是:

<Window.Resources> 
    <Style TargetType="TextBox" x:Key="enableDisableStyle"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding IsChecked, ElementName=disabledOne}" Value="True"> 
       <Setter Property="IsEnabled" Value="False" /> 
      </DataTrigger> 
      <DataTrigger Binding="{Binding IsChecked, ElementName=disabledTwo}" Value="True"> 
       <Setter Property="IsEnabled" Value="False" /> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</Window.Resources> 
<StackPanel> 
    <RadioButton GroupName="group" x:Name="enabledOne">Enabled one</RadioButton> 
    <RadioButton GroupName="group" x:Name="enabledTwo">Enabled two</RadioButton> 
    <RadioButton GroupName="group" x:Name="disabledOne">Disabled one</RadioButton> 
    <RadioButton GroupName="group" x:Name="disabledTwo">Disabled two</RadioButton> 

    <TextBox Text="Test" Style="{StaticResource enableDisableStyle}" /> 
</StackPanel> 

我不會慶祝我自己的答案是接受,直到我相信這是最好的解決方案。