从事件触发更改时MVVM绑定不起作用

时间:2017-10-30 11:25:19

标签: c# wpf mvvm

我制作了一个简单的MVVM示例。我有两个用户控制页面的主窗口。主窗口有两个事件将视图更改为两个用户控件。

这是我的主窗口XAML

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MVVC_Binding"
        xmlns:views="clr-namespace:MVVC_Binding.Views"
        xmlns:viewModel="clr-namespace:MVVC_Binding.ViewModels"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" x:Class="MVVC_Binding.MainWindow"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type viewModel:Page1ViewModel}">
            <views:Page1 />
        </DataTemplate>

        <DataTemplate DataType="{x:Type viewModel:Page2ViewModel}">
            <views:Page2/>
        </DataTemplate>
    </Window.Resources>
    <Window.DataContext>
        <local:MainViewModel />
    </Window.DataContext>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0">
            <TextBlock Text="Page1">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseLeftButtonUp">
                        <i:InvokeCommandAction Command="{Binding NavCommand}" CommandParameter="page1"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </TextBlock>
            <TextBlock Text="Page2">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseLeftButtonUp">
                        <i:InvokeCommandAction Command="{Binding NavCommand}" CommandParameter="page2"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </TextBlock>
        </StackPanel>
        <DockPanel Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Top" Background="Gainsboro">
            <Grid x:Name="container" Background="Gainsboro" VerticalAlignment="Top">
                <ContentControl Content="{Binding CurrentViewModel}"/>
            </Grid>
        </DockPanel>
    </Grid>
</Window>

我正在为我的视图模型使用BindableBase类。这是我的BindableBase类

namespace MVVC_Binding.Utilities
{
    public class BindableBase : INotifyPropertyChanged
    {
        /// <summary>
        /// Interface implementation
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        protected virtual void SetProperty<T>(ref T member, T val, [CallerMemberName] string propertyName = null)
        {
            // Check for current set member and the new value
            // If they are the same, do nothing
            if (object.Equals(member, val)) return;

            member = val;
            // Invoke the property change event
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        protected virtual void OnPropertyChanged(string propName)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    }
}

在我的主视图模型中,只需简单点击事件即可更改视图绑定

private BindableBase _CurrentViewModel;

        public BindableBase CurrentViewModel
        {
            get { return _CurrentViewModel; }
            set
            {
                SetProperty(ref _CurrentViewModel, value);
            }
        }

        private void OnNav(string destination)
        {
            switch (destination)
            {
                case "page2":
                    CurrentViewModel = page2;
                    break;
                default:
                    CurrentViewModel = page1;
                    break;
            }
        }

问题在于用户控件第2页,当它显示时,它旁边的事件不会改变TextBlock绑定值,但文本可以在视图模型构造函数事件期间更改。

这是我的第2页XAML

<UserControl x:Class="MVVC_Binding.Views.Page2"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MVVC_Binding.Views"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel>
        <TextBlock Name="txtPage2" Text="{Binding Page2Text}"></TextBlock>
        <TextBlock Name="btn" Text="Click Button">
            <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseLeftButtonUp">
                        <i:InvokeCommandAction Command="{Binding BtnCommand}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
        </TextBlock>
    </StackPanel>
</UserControl>

这是Page2ViewModel

namespace MVVC_Binding.ViewModels
{
    public class Page2ViewModel : BindableBase
    {
        public MyICommand<string> BtnCommand { get; private set; }

        public Page2ViewModel()
        {
            BtnCommand = new MyICommand<string>(OnBtnClick);
            Page2Text = "Just a test";
        }

        private void OnBtnClick(string obj)
        {
            Page2Text = "Changing by button click";
        }

        private string _page2Text;

        public string Page2Text
        {
            get { return _page2Text; }
            set
            {
                _page2Text = value;
                SetProperty(ref _page2Text, value);
            }
        }
    }
}
你能看到我做错了吗?非常感谢

2 个答案:

答案 0 :(得分:2)

如果我理解正确,您就会问为什么此功能中的代码似乎不会对视图产生影响:

private void OnBtnClick(string obj)
{
    _page2Text = "Changing by button click";
}

问题是您正在更改基础_page2Text成员,但为了让WPF检测到此更改,您必须使用Page2Text属性,如下所示:

private void OnBtnClick(string obj)
{
    Page2Text = "Changing by button click";
}

代码中指示属性更改为WPF的特定部分是OnPropertyChanged类中的BindableBase方法。

答案 1 :(得分:1)

谢谢大家,我设法通过更新BindableBase类来解决它。更新

namespace SilentUpdate.Utilities
{
    public class BindableBase : INotifyPropertyChanged
    {
        /// <summary>
        /// Interface implementation
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void SetProperty<T>(ref T member, T val, [CallerMemberName] string propertyName = null)
        {
            // Check for current set member and the new value
            // If they are the same, do nothing
            // if (object.Equals(member, val)) return;

            member = val;
            // Invoke the property change event
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        protected virtual void OnPropertyChanged(string propName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
        }
    }
}

所以我注释掉Object.Equals的检查并强制它为每个值运行