0

这是我的模型

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>

问题是,如果我单击用户控件内的按钮,它会刷新更改,但是当我尝试从主窗口单击按钮时,没有发生同样的事情。问题是什么?

4

1 回答 1

1

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

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

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

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

于 2012-05-29T17:37:47.910 回答