微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我试图创建一个用依赖属性来绑定的用户控件.在内部我有一个绑定到这些相同属性的ComboBox,但绑定只能以一种方式工作. ComboBox从ItemsSource填充,但SelectedItem没有被更新回到我绑定的viewmodel.

一个简化的例子:

这是与用户控件绑定的视图模型:

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),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>

用户控件中的组合框填写名称,但是当我选择一个不同的名称时,它不会被更新回到视图模型.任何想法我在这里失踪?

谢谢!

一些属性认绑定双向(包括SelectedItem),但您的BoundSelectedPerson不.您可以设置绑定的模式:
<controls:PeopleControl 
            BoundPeople="{Binding People}" 
            BoundSelectedPerson="{Binding SelectedPerson,Mode=TwoWay}"/>

或者您可以通过在DependencyProperty上设置一个标志来认为TwoWay:

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

原文地址:https://www.jb51.cc/javaschema/281911.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐