0

我在一个例子中试过这个,它似乎工作正常。然后我将它应用到我们正在进行的主要项目中,但它失败了。这是相关的代码片段:

<ComboBox Name="Combo" IsSynchronizedWithCurrentItem="True" 
ItemsSource="{Binding Tables}" SelectedItem="{Binding TableName, Mode=TwoWay}" 
Style="{StaticResource ComboBoxStyle}" Grid.Row="1" Height="30" Width="180" 
SelectionChanged="Combo_SelectionChanged" IsEnabled="{Binding IsChecked, ElementName=rdBtnList}" Margin="6,20,6,0" Grid.RowSpan="2" />

以及控制它的单选按钮:

<RadioButton Content="By List" Height="16" IsChecked="{Binding Path=ListSelect, Mode=TwoWay}" 
HorizontalAlignment="Right" Margin="0,6,24,0"  Name="rdBtnList" VerticalAlignment="Top" 
Background="DodgerBlue" FontSize="13" FontWeight="Bold" Grid.RowSpan="2" />

有没有人看到任何看起来不正确的东西(关于 IsEnabled 的情况)?

4

1 回答 1

1

目前,在选择“广播”按钮时,启用了代码的编写方式。If you want the opposite to happen, when the radio button is selected the combo box should be disabled, you need a converter to make the boolean value the opposite.

public class OppositeBoolConverter : IValueConverter {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            return !System.Convert.ToBoolean(value);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
            return value;
        }
    }
}

现在,添加一个命名空间引用并将转换器添加到组合框的绑定中:

xmlns:local="clr-namespace:NAMESPACE"

<local:OppositeBoolConverter x:Key="cnt" />

IsEnabled="{Binding IsChecked, Converter={StaticResource cnt}, ElementName=rdBtnList}"
于 2013-03-27T14:04:30.673 回答