WPF数据绑定问题 - 可能的菜鸟问题 [英] WPF DataBinding Issues - Possible Noob Problems

查看:150
本文介绍了WPF数据绑定问题 - 可能的菜鸟问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想类型能见度的视图模型属性绑定到一个固定面板visibility属性:

I am trying to bind a ViewModel property of type Visibility to the visibility property on a Dock Panel:

更新视图模型code

Updated ViewModel Code:

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));
        }
    }
}

这里是我更新XAML code

and here is my updated XAML code:

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

和我这条线设置在code中的数据上下文背后:

and I set the data context in the code behind with this line:

    tvwDockPanel.DataContext = btnSelectWaferViewModel;

其中btnSelectWaferViewModel是这种情况的视图模型对象。

where btnSelectWaferViewModel is the ViewModel object for this situation.

并为乐趣,这里是我的背后code

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
}

所有我想要做的点击按钮btnSelectWafer并有tvwDockPanel的可见性属性来获取通过结合设置为可见。然后,当你在btnSelectWafer再次点击,tvwDockPanel的可见性属性,都会设置回折叠一次。 tvwDockPanel的知名度将永远只能要么倒塌或可见。

All I want to do click the button btnSelectWafer and have the tvwDockPanel's visibility property to get to set to Visible via binding. Then when you click again on btnSelectWafer, tvwDockPanel's visibility property gets set back to Collapsed again. tvwDockPanel's visibility will only ever be either Collapsed or Visible.

任何帮助将是真棒,我是相当新的这整个数据绑定的概念。

Any help would be awesome, I am rather new to this whole data binding concept.

推荐答案

您有几个问题在这里:

首先,MVVM的意图(如果你想与MVVM这样做)是从presentation 独立的逻辑。这意味着,在没有办法的视图模型可以有一个参考 System.Windows.Controls.Button ,也没有 System.Windows.Visibility ,也没有到 System.Windows 命名空间内的任何其他类。

First of all, the intent of MVVM (if you're trying to do this with MVVM) is to separate logic from 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.

这是我不太清楚你的 SelectWaferButtonViewModel 类与按钮做,但你需要从那里删除的按钮。

It is not clear to me what your SelectWaferButtonViewModel class is doing with the Button, but you need to remove the Button from there.

另外,如果你需要从视图模型层操作控制的知名度,你最好使用布尔属性和 BooleanToVisibilityConverter 在XAML:

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:

视图模型:

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

XAML:

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

这篇关于WPF数据绑定问题 - 可能的菜鸟问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆