WPF无法使用MVVM从主窗口刷新usercontrol

时间:2012-05-29 13:43:33

标签: wpf wpf-controls

  

这是我的模特

public class PersonModel : INotifyPropertyChanged
{ 
    private string _firstname;
    public string FirstName 
    { 
        get {return _firstname; }
        set { _firstname = value; RaisePropertyChanged("FirstName"); } 
    }

    private string _lastname;
    public string LastName
    {
        get { return _lastname; }
        set { _lastname = value; RaisePropertyChanged("LastName"); }
    } 

    public string FullName 
    {
        get { return string.Format("{0} {1}", FirstName, LastName); } 
    }

    protected void RaisePropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
} 
  

我的视图基本上是用户控件

<UserControl x:Class="WpfApplication1.UserControl1"
         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:WpfApplication1"             
         mc:Ignorable="d"            
         d:DesignHeight="300" d:DesignWidth="300"                
         Visibility="{Binding HideStatus, Mode=TwoWay}">
<UserControl.DataContext>
    <local:PersonViewModel></local:PersonViewModel>
</UserControl.DataContext>  
     <Grid>      
    <Grid.RowDefinitions>
        <RowDefinition Height="117*"></RowDefinition>
        <RowDefinition Height="33*"></RowDefinition>
        <RowDefinition Height="75*"></RowDefinition>
        <RowDefinition Height="75*"></RowDefinition>
    </Grid.RowDefinitions>
    <TextBlock Grid.Row="0" Text="FirstName"></TextBlock>
    <TextBox Text="{Binding FirstName,Mode=TwoWay}" Name="txtFirstName" Margin="115,0,0,67" DataContext="{Binding Path=Person}" Background="#FFF0F0F0" />
    <TextBlock Text="FirstName" Margin="0,71,0,0" Grid.RowSpan="2"></TextBlock>
    <TextBox Text="{Binding LastName,Mode=TwoWay}" Name="txtLastName" Margin="115,90,0,12" Background="#FFF0F0F0" DataContext="{Binding Path=Person}" Grid.RowSpan="2" />
    <Button Grid.Row="3" Content="Button" Height="29" HorizontalAlignment="Left" Margin="12,23,0,0" Name="button1" VerticalAlignment="Top" Width="75" Command="{Binding UpdateView}" />
</Grid>
  

在主窗口中有一个按钮“从主UI更新”和我们已经创建的用户控件

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="MainWindow" Height="350" Width="525"

    xmlns:model="clr-namespace:WpfApplication1"
    >
<Grid>
    <Grid.DataContext>
        <local:PersonViewModel/>
    </Grid.DataContext>
    <local:UserControl1 x:Name="control1" />
    <Button Content="Update From Main UI" Height="23" Command="{Binding UpdateView}" HorizontalAlignment="Left" Margin="358,276,0,0" Name="button1" VerticalAlignment="Top" Width="145" />
</Grid>

  

问题是如果我点击usercontrol里面的按钮刷新更改,但当我试图从主窗口点击按钮时,同样的事情就不会发生。有什么问题?

1 个答案:

答案 0 :(得分:0)

主窗口的DataContext是另一个PersonViewModel的类实例,而不是UserControl的DataContext。因此,从主窗口执行UpdateView不会更新UserControl。

您应该在主窗口中写下这个:

<Button Command="{Binding Path=DataContext.UpdateView, ElementName=control1}" ... />

通过ElementName,告诉Binding引用用户控件。通过DataContext.UpdateView,您将引用用户控件的UpdateView命令。

相关问题