简体   繁体   English

WPF数据绑定问题-可能的Noob问题

[英]WPF DataBinding Issues - Possible Noob Problems

I am trying to bind a ViewModel property of type Visibility to the visibility property on a Dock Panel: 我正在尝试将类型Visibility的ViewModel属性绑定到Dock面板上的visible属性:

Updated ViewModel Code : 更新的ViewModel代码

public class SelectWaferButtonViewModel : INotifyPropertyChanged
{
    private bool isClicked;

    public SelectWaferButtonViewModel()
    {
        isClicked = false;
    }

    public bool IsControlVisible
    {
        get
        {
            return isClicked;
        }
        set
        {
            isClicked = value;
            OnPropertyChanged("IsControlVisible");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnButtonClick()
    {
        if (isClicked)
        {
            IsControlVisible = false;
        }
        else
        {
            IsControlVisible = true;
        }
    }
    protected virtual void OnPropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    }
}

and here is my updated XAML code : 这是我更新的XAML代码

            <DockPanel
                Name="tvwDockPanel"
                Width="200"
                Visibility="{Binding IsControlVisible, FallbackValue=Collapsed, Converter={StaticResource BoolToVisConverter}}"
                DockPanel.Dock="Left">
                <DockPanel
                    DockPanel.Dock="Top"
                    Height="22">
                </DockPanel>

and I set the data context in the code behind with this line: 我在此行后面的代码中设置了数据上下文:

    tvwDockPanel.DataContext = btnSelectWaferViewModel;

where btnSelectWaferViewModel is the ViewModel object for this situation. 其中btnSelectWaferViewModel是这种情况下的ViewModel对象。

and for fun, here is my code behind : 为了好玩,这是我的代码背后

public partial class WaferTrackerWindow : Window
{
    List<ISubscribeEvents> subscriptionList;
    SelectWaferButtonViewModel btnSelectWaferViewModel;

    public WaferTrackerWindow()
    {
        InitializeComponent();

        this.InstantiateObjects();
        this.SubscribeEvents();
        this.SetDataContexts();
    }

    #region Methods

    private void SetDataContexts()
    {
        tvwDockPanel.DataContext = btnSelectWaferViewModel.IsControlVisible;
    }
    private void SubscribeEvents()
    {
        foreach (ISubscribeEvents subscriber in subscriptionList)
        {
            subscriber.SubscribeEvents();
        }
    }
    private void InstantiateObjects()
    {
        btnSelectWaferViewModel = new SelectWaferButtonViewModel();
        subscriptionList = new List<ISubscribeEvents>();
        subscriptionList.Add(
            new Classes.WaferTrackerWindow.SelectWaferButtonView(btnSelectWafer, btnSelectWaferViewModel));
    }

    #endregion
}

All I want to do click the button btnSelectWafer and have the tvwDockPanel's visibility property to get to set to Visible via binding. 我要做的就是单击btnSelectWafer按钮,并通过绑定将tvwDockPanel的visible属性设置为Visible。 Then when you click again on btnSelectWafer, tvwDockPanel's visibility property gets set back to Collapsed again. 然后,当您再次单击btnSelectWafer时,tvwDockPanel的可见性属性将再次设置为Collapsed。 tvwDockPanel's visibility will only ever be either Collapsed or Visible. tvwDockPanel的可见性将永远不会被折叠或可见。

Any help would be awesome, I am rather new to this whole data binding concept. 任何帮助都将是很棒的,对于整个数据绑定概念我还是很陌生。

You have several issues here: 您在这里遇到几个问题:

First of all, the intent of MVVM (if you're trying to do this with MVVM) is to separate logic from presentation . 首先,MVVM的目的(如果要使用MVVM做到这一点)是将逻辑与presentation分开 This means that in no way your ViewModel can have a reference to System.Windows.Controls.Button , nor to System.Windows.Visibility , nor to any other classes inside the System.Windows Namespace. 这意味着您的ViewModel绝不能引用System.Windows.Controls.ButtonSystem.Windows.VisibilitySystem.Windows命名空间内的任何其他类。

It is not clear to me what your SelectWaferButtonViewModel class is doing with the Button, but you need to remove the Button from there. 我不清楚您的SelectWaferButtonViewModel类对Button做了什么,但是您需要从那里删除Button。

Also, If you need to manipulate the Visibility of a control from the ViewModel layer, you'd better use a Boolean property and the BooleanToVisibilityConverter in XAML: 另外,如果需要从ViewModel层操纵控件的可见性,则BooleanToVisibilityConverter在XAML中使用Boolean属性和BooleanToVisibilityConverter

ViewModel: ViewModel:

public bool IsControlVisible {get;set;} //Don't forget INotifyPropertyChanged!!

XAML: XAML:

<Window.Resources>
    <BooleanToVisibilityConverter x:Key="BoolToVisConverter"/>
</Window.Resources>
<DockPanel Visibility="{Binding IsControlVisible, Converter={StaticResource BoolToVisConverter}}"/>

The problem is that you're binding your DockPanel to the boolean property of your view model, and then setting the Visiblity property of your UI element to the IsControlVisible property of the datacontext (which doesn't exist). 问题是您将DockPanel绑定到视图模型的boolean属性,然后将UI元素的Visiblity属性设置为datacontext的IsControlVisible属性(不存在)。

Change to: 改成:

private void SetDataContexts()
{
    tvwDockPanel.DataContext = btnSelectWaferViewModel;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM