将按钮的属性绑定到静态字段类(MVVM)

时间:2013-12-20 07:02:36

标签: c# wpf xaml mvvm

我的主窗口上有按钮,我想通过静态字段类启用和禁用该控件。这可能没有去后面的代码吗?

主窗口

<Button x:Name="btnAanpassen" Content="Aanpassen" Grid.Row="1" Command="{Binding SaveItemCommand}" CommandParameter="{Binding SelectedItem}" IsEnabled="{Binding EnableDisable}"/>

我的vm

    private static object _selectedItem;
    public static object SelectedItem
    {
        get { return _selectedItem; }
        set {
            if (SelectedItem != null)
            {
                //enable control
            }
            else
            {
                //disable control
            }
            _selectedItem = value; }
    }

    private Boolean _enableDisable;
    public Boolean EnableDisable
    {
        get { return _enableDisable; }
        set { _enableDisable = value; OnPropertyChanged("EnableDisable"); }
    }

4 个答案:

答案 0 :(得分:0)

你可以试试这个:

<Window.Resources>
    <yourNS:YourMVVM x:Key="mvvm"/>
</Window.Resources>

...

<Button x:Name="btnAanpassen" Content="Aanpassen" Grid.Row="1" Command="{Binding SaveItemCommand}" 
        CommandParameter="{Binding Source={StaticResource mvvm}, Path=SelectedItem}" IsEnabled="{Binding EnableDisable}"/>

答案 1 :(得分:0)

在这种情况下使用Static Property存在一些问题。我有一个关于这个问题的解决方法。请参阅以下片段适合您。

public partial class MainWindow : Window,INotifyPropertyChanged 
{
    public event PropertyChangedEventHandler PropertyChanged;
    public static object mySharedVariableToOtherClass = value;
    private object _selectedItem;

    public object SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            mySharedVariableToOtherClass = null;
            OnPropertyChanged("SelectedItem");
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
        SelectedItem = "vimal";
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        SelectedItem = null;
    }
    public void OnPropertyChanged(string PropertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(PropertyName));    
        }
    }
}

在上面的代码段中,我将SelectedItem属性声明为普通Property,用于绑定UIElement。我还声明了一个名为Property的{​​{1}} mySharedVariableToOtherClass,用于设置Static属性中的值。这是SelectedItem,因此您也可以从其他类访问它。在我的公开Static是禁用Trigger控件的正确选择。

Button

答案 2 :(得分:0)

尝试以下链接中提到的方式

WPF TwoWay binding to a static property

答案 3 :(得分:0)

您无法直接绑定到静态属性。 如果您确实需要此属性为静态,则一种方法是创建一个获取并设置它的代理属性。它基于singleton pattern

private static readonly ClassName _this = new ClassName();

public object ProxySelectedItem
{
    get { return SelectedItem; }
    set { SelectedItem = value; }
}

private static object _selectedItem;
public static object SelectedItem
{
    get { return _selectedItem; }
    set
    {
        if (SelectedItem != null)
        {
            _this.EnableDisable = true;
        }
        else
        {
            _this.EnableDisable = false;
        }
        _selectedItem = value;
        _this.OnPropertyChanged("ProxySelectedItem");
    }
}

private bool _enableDisable;
public bool EnableDisable
{
    get { return _enableDisable; }
    set 
    {
        _enableDisable = value;
        OnPropertyChanged("EnableDisable"); 
    }
}