如何使我的WPF用户控件的依赖属性更新我的视图模型?

15
我试图创建一个带有依赖属性的用户控件进行绑定。在内部,我有一个 ComboBox 绑定到这些相同的属性,但是绑定只能单向工作。ComboBox 从 ItemsSource 填充,但 SelectedItem 不会更新回我要绑定的视图模型。
下面是可简化的示例:
这是要与用户控件绑定的视图模型:
public class PeopleViewModel : INotifyPropertyChanged
{
    public PeopleViewModel()
    {
        People = new List<string>( new [] {"John", "Alfred","Dave"});
        SelectedPerson = People.FirstOrDefault();
    }

    public event PropertyChangedEventHandler PropertyChanged;

    private IEnumerable<string> _people;
    public IEnumerable<string> People 
    { 
        get { return _people; } 
        set
        {
            _people = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("People"));
            }
        }
    }

    private string _selectedPerson;
    public string SelectedPerson 
    {
        get { return _selectedPerson; }
        set 
        { 
            _selectedPerson = value;
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("SelectedPerson"));
            }
        } 
    }
}

这是用户控件:

<UserControl x:Class="PeopleControlTest.PeopleControl"
         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" 
         mc:Ignorable="d" d:DesignHeight="56" d:DesignWidth="637">
<StackPanel >
    <ComboBox Margin="11"
              ItemsSource="{Binding BoundPeople, RelativeSource={RelativeSource AncestorType=UserControl}}"
              SelectedItem="{Binding BoundSelectedPerson, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
</StackPanel>

使用代码后端

public partial class PeopleControl : UserControl
{
    public PeopleControl()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty BoundPeopleProperty =
        DependencyProperty.Register("BoundPeople", typeof(IEnumerable<string>), typeof(PeopleControl), new UIPropertyMetadata(null));

    public static readonly DependencyProperty BoundSelectedPersonProperty =
        DependencyProperty.Register("BoundSelectedPerson", typeof(string), typeof(PeopleControl), new UIPropertyMetadata(""));

    public IEnumerable<string> BoundPeople
    {
        get { return (IEnumerable<string>)GetValue(BoundPeopleProperty); }
        set { SetValue(BoundPeopleProperty, value); }
    }

    public string BoundSelectedPerson
    {
        get { return (string)GetValue(BoundSelectedPersonProperty); }
        set { SetValue(BoundSelectedPersonProperty, value); }
    }
}

这是我如何在主窗口中将用户控件绑定(将Windows数据上下文设置为viewmodel的实例)

<Window x:Class="PeopleControlTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:controls="clr-namespace:PeopleControlTest"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <controls:PeopleControl 
            BoundPeople="{Binding People}" 
            BoundSelectedPerson="{Binding SelectedPerson}"/>
    </Grid>
</Window>

用户控件中的组合框填充了名称,但是当我选择不同的名称时,它并没有更新回视图模型。你有什么想法这里缺少了什么吗?

谢谢!

1个回答

20

有些属性默认会进行双向绑定(包括 SelectedItem),但是您的 BoundSelectedPerson 没有。您可以设置绑定的模式:

   <controls:PeopleControl 
            BoundPeople="{Binding People}" 
            BoundSelectedPerson="{Binding SelectedPerson, Mode=TwoWay}"/>

或者您可以在DependencyProperty上设置一个标志,使其默认为双向绑定:

    public static readonly DependencyProperty BoundSelectedPersonProperty =
        DependencyProperty.Register("BoundSelectedPerson", typeof(string), typeof(PeopleControl), new FrameworkPropertyMetadata("",FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

太好了,谢谢,有效!!!由于某些原因,我以为所有的DP默认都是双向工作的。我也使用UIPropertyMetadata,在那里我找不到那个选项。 - user348905
这个解决方案很好。我只想为那些可能遇到同样问题并在寻找解决方案时来到这里的人添加一条注释。绑定到UserControl的依赖属性的属性必须具有set()方法。 - skst

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接