通过ViewModel绑定到DependencyProperty无效

时间:2012-09-26 06:21:26

标签: wpf mvvm binding dependency-properties code-behind

我简化了我的问题(这意味着原理类似,但我不想将int转换为字符串......):

MainWindow.xaml:

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

        <WpfApplication1:UserControl1 CurrentNumber="{Binding Path=TheSpecialNumber, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

    </Grid>
</Window>

在主窗口中出现UserControl1。 usercontrol具有属性Current Number。我想绑定属性TheSpecialNumber(ViewModel)。

MainWindow.xaml.cs:

namespace WpfApplication1
{
    public partial class MainWindow
    {
        private readonly ViewModel _viewModel;

        public MainWindow()
        {
            InitializeComponent();
            _viewModel = new ViewModel();
            DataContext = _viewModel;

            _viewModel.TheSpecialNumber = "8";
            _viewModel.UpdateTheSpecialNumberBinding();
        }
    }
}

ViewModel.cs:

namespace WpfApplication1
{
    class ViewModel : INotifyPropertyChanged
    {
        public string TheSpecialNumber { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;

        public void UpdateTheSpecialNumberBinding()
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("TheSpecialNumber"));
            }
        }
    }
}

UserControl1.xaml:

<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"
             mc:Ignorable="d"
             Height="200" Width="300" Background="Aqua">
    <Grid Name="container">
        <ComboBox SelectedIndex="{Binding Path=CurrentNumberIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
            <ComboBoxItem Height="20">0</ComboBoxItem>
            <ComboBoxItem Height="20">1</ComboBoxItem>
            <ComboBoxItem Height="20">2</ComboBoxItem>
            <ComboBoxItem Height="20">3</ComboBoxItem>
            <ComboBoxItem Height="20">4</ComboBoxItem>
            <ComboBoxItem Height="20">5</ComboBoxItem>
            <ComboBoxItem Height="20">6</ComboBoxItem>
            <ComboBoxItem Height="20">7</ComboBoxItem>
            <ComboBoxItem Height="20">8</ComboBoxItem>
            <ComboBoxItem Height="20">9</ComboBoxItem>
        </ComboBox>
    </Grid>
</UserControl>

UserControl是一个ComboBox。这有10件。我想始终显示与属性当前编号具有相同编号的项目。因此,当前数字索引的绑定。

UserControl1.xaml.cs:

using System;
using System.ComponentModel;
using System.Windows;

namespace WpfApplication1
{
    public partial class UserControl1 : INotifyPropertyChanged
    {
        public static readonly DependencyProperty CurrentNumberProperty = DependencyProperty.Register("CurrentNumber", typeof(string), typeof(UserControl1));

        public UserControl1()
        {
            InitializeComponent();
            container.DataContext = this;

            CurrentNumber = "5";
        }

        public string CurrentNumber
        {
            get { return (string)GetValue(CurrentNumberProperty); }
            set
            {
                SetValue(CurrentNumberProperty, value);
                OnPropertyChanged("CurrentNumberIndex");
            }
        }

        public int CurrentNumberIndex
        {
            get
            {
                return Convert.ToInt32(CurrentNumber);
            }
            set
            {
                CurrentNumber = Convert.ToString(value);
            }
        }

        #region Implementation of INotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion Implementation of INotifyPropertyChanged
    }
}

现在我的问题。为什么只显示内容为0的ComboBox项目,如果我在MainWindow(通过ViewModel)中设置8?即使我在当前号码的设定者中设置了一个断点,我只停止一次,即在5,但不是在8。

3 个答案:

答案 0 :(得分:3)

你可以通过设置CurrentNumber = 5 manuelly来“破坏”TheSpecialNumber和CurrentNumber之间的绑定。删除此部分,然后重试。

编辑: 好的,如果这不起作用,请尝试以下方法:

  1. 将新的PropertyChangedCallback添加到PropertyMetaData:

    public static readonly DependencyProperty CurrentNumberProperty = DependencyProperty.Register("CurrentNumber", typeof(string), typeof(UserControl1), new PropertyMetadata( new PropertyChangedCallback(CurrentNumberPropertyChanged))); 
    
  2. 在此方法中设置CurrentNumber,如:

    private static void CurrentNumberPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        UserControl1 uc1 = (UserControl1)dependencyObject;
        uc1.CurrentNumber = (string)dependencyPropertyChangedEventArgs.NewValue;
    }
    

答案 1 :(得分:1)

CurrentNumber是WPF属性,这意味着如果WPF使用该属性,它将传递CurrentNumber的getter / setter。

所以基本上当你设置TheSpecialNumber时,这部分不会被执行:

 SetValue(CurrentNumberProperty, value);
 OnPropertyChanged("CurrentNumberIndex");

将自己连接到WPF properties UI属性元数据中。

答案 2 :(得分:0)

<ComboBox SelectedIndex="{Binding Path=CurrentNumberIndex, RelativeSource={RelativeSource Mode=Self}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

在SelectedIndex的绑定中,绑定系统将在UserControl DataContext中查找CurrentNumberIndex属性,这是您的ViewModel,并且它不会在那里找到此属性,以便将它设置为将SelfSource设置为Self。我希望这会有所帮助。