WPF绑定IsEnabled无法正常工作(内部DataContext)

时间:2016-09-14 13:48:00

标签: c# wpf data-binding

我有一个带有属性的对话窗口,在xaml中我放了:

DataContext="{Binding RelativeSource={RelativeSource Self}}">

并尝试将Grid IsEnabled 绑定到我的对话框类中抵制的属性,如下所示:

//xaml
<Grid Margin="10,0,0,10" Name="DevicePropertiesGrid" IsEnabled="{Binding Path=isFixedMode}">

//source
public bool isFixedMode 
{ 
    get { return ModeComboBox.SelectionBoxItem.ToString().Equals("Fixed"); } 
}

没有结果, isFixedMode 切换它的值,但对Grid没有影响。

2 个答案:

答案 0 :(得分:2)

更新值时,您需要notify您的观点。

在你的情况下,我可能会编写经典的get / set属性,并在我的选择项更改时更新它。

当然,您的ViewModel必须是 INotifyPropertyChanged 。然后你可以做类似的事情:

    private bool _isFixedMode;
    public bool IsFixedMode 
    {
        get { return _isFixedMode; }
        set
        {
            _isFixedMode= value;
            if(PropertyChanged != null)
                PropertyChange(this, new PropertyChangedEventArgs("IsFixedMode ")); 
        }
    }

然后您注册到事件 OnSelectionBoxItemChanged ,并且每次调用它时,您都会重新设置IsFixedMode值,如下所示:

   IsFixedMode = ModeComboBox.SelectionBoxItem.ToString().Equals("Fixed"); 

答案 1 :(得分:1)

因为我提到了IValueConverter以下是我认为应该如何完成这种情况。 xaml中的代码

<StackPanel>
        <StackPanel.Resources>
            <local:IsFixedValueConverter x:Key="IsFixedValueConverter"/>
        </StackPanel.Resources>
        <ComboBox Margin="10" Width="120" SelectedValue="{Binding SelectionItem}">
            <sys:String>Fixed</sys:String>
            <sys:String>NotFixed</sys:String>
        </ComboBox>

        <CheckBox Margin="10" Width="120"  IsChecked="{ Binding SelectionItem, Converter={ StaticResource IsFixedValueConverter} }"/>

        <TextBox Margin="10" Width="120"  IsEnabled="{ Binding SelectionItem, Converter={ StaticResource IsFixedValueConverter } }"/>

    </StackPanel>

和C#代码

 public partial class MainWindow : Window
    {
        //The window is just so we have something to set a VM on if you DataContext is Self you can add the ViewModel code to the UI element (But you really should use ViewModels)
        public MainWindow()
        {
            InitializeComponent();

            DataContext = new VM();
        }


    }

    public class VM:INotifyPropertyChanged 
    {
        private string selectionItem = IsFixedValueConverter.IS_FIXED;
        public string SelectionItem 
        {
            get { return selectionItem; }
            set
            {
                selectionItem = value;
                if (PropertyChanged != null)//If you are using c# 6 use nameof(SelectionItem) rather than "SelectionItem"
                    PropertyChanged(this, new PropertyChangedEventArgs("SelectionItem"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

    public class IsFixedValueConverter : IValueConverter 
    {
        public const string IS_FIXED = "Fixed";

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value!= null?value.ToString() == IS_FIXED:false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return ((bool)value) ? IS_FIXED : null;
        }
    } 

这只是一个推荐