【问题标题】:DependencyProperty ... can set, but can't get value out via bindingDependencyProperty ... 可以设置,但无法通过绑定获取值
【发布时间】:2011-12-07 01:43:51
【问题描述】:

我有一个窗口,其中包含一个用户控件“模板编辑器”。 (缩短的)TemplateEditor XAML 是:

<UserControl x:Class="xxx.Windows.Core.Controls.TemplateEditor"
             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" 
             x:Name="userControl">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TextBox Grid.Row="1" x:Name="textBox" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" />
    </Grid>
</UserControl>

我希望能够通过 TemplateEditor 将数据绑定到 TextBox“textBox”中。我正在使用 DependencyProperty 来屏蔽代码隐藏中的 TextBox:

namespace xxx.Windows.Core.Controls
{
    public partial class TemplateEditor : UserControl 
    {
        public string Text
        {
            get 
            { 
                string s=(string)GetValue(TextProperty);
                return s;
            }
            set 
            {
                if (Text != value)
                { 
                    SetValue(TextProperty, value);
                }
            }
        }

        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register("Text", typeof(string), typeof(TemplateEditor),new PropertyMetadata(null,Text_PropertyChanged));

        public TemplateEditor()
        {
            InitializeComponent();
        }

        private static void Text_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
        {
            ((TemplateEditor)source).textBox.Text = (string)e.NewValue;
        } 
    }
}

因此,您可以从中看到我有一个 DependencyProperty Text,我有一个回调来获取对绑定所做的更改(例如,来自 ViewModel)并应用于 TextBox 值。

这行得通。

问题是,我似乎无法反向进行绑定工作,即。将值从 Text 属性(因此也是 TextBox)中取回并返回到绑定使用者(ViewModel)。我已经调试过调用 GetValue(TextProperty) 并返回正确的值,因此 DP 字典正确更新。

在父窗口的 XAML 中给出以下绑定:

<src:ApplicationWindowBase x:Class="xxx.Windows.Client.Filing"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:src="clr-namespace:xxx.Windows.Core;assembly=MIGTurbo1.Windows.Core"
        xmlns:viewmodel="clr-namespace:xxx.Windows.Client.ViewModel"
        xmlns:converters="clr-namespace:xxx.Windows.Client.Converters"
        xmlns:xxx_Windows_Core_Controls="clr-namespace:xxx.Windows.Core.Controls;assembly=xxx.Windows.Core" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d" 
        Title="File Item(s)" Height="450" Width="550" ShowInTaskbar="False">
    <src:ApplicationWindowBase.Resources>
        <viewmodel:ViewModelLocator x:Key="ViewModelLocator" d:IsDataSource="True"/>
        <converters:FileableItemNameStringConverter x:Key="fileableItemNameStringConverter" />
        <converters:FileableItemTypeStringConverter x:Key="fileableItemTypeStringConverter" />
        <converters:FileableItemMetaDataStringConverter x:Key="fileableItemMetaDataStringConverter" />
        <converters:FileableItemIconConverter x:Key="fileableItemIconConverter" />
    </src:ApplicationWindowBase.Resources>
    <src:ApplicationWindowBase.DataContext>
        <Binding Mode="OneWay" Path="Filing" Source="{StaticResource ViewModelLocator}"/>
    </src:ApplicationWindowBase.DataContext>
    <Grid>
       <!-- SNIP -->
    <xxx_Windows_Core_Controls:TemplateEditor Grid.Column="1" Grid.Row="1" Margin="0" Text="{Binding Comment, Mode=TwoWay}" d:LayoutOverrides="Width, Height"/>
       <!-- SNIP -->
</src:ApplicationWindowBase>

我正在使用 MVVM Light 并且 ViewModel 确实绑定正确。还有其他可以正常工作的控件/字段绑定(省略)。问题是 Comment ViewModel 属性上的 Text 属性绑定不起作用。我可以很好地设置它(即在 ViewModel 中设置值,然后绑定),但 Text 中用户输入的值永远不会进入 ViewModel 的 Comments 属性。

我做错了什么?

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    试试这个:

    <TextBox Text="{Binding ElementName=userControl,Path=Text,Mode=TwoWay}" />
    

    并删除处理程序。

    【讨论】:

      【解决方案2】:

      好的,典型的StackOverflow usage pattern被采纳了。

      我意识到我在写这个单向,添加了一个 TextChanged 事件处理程序来更新依赖属性。

      public partial class TemplateEditor : UserControl 
      {
      
          public string Text
          {
              get 
              { 
                  string s=(string)GetValue(TextProperty);
                  return s;
              }
              set 
              {
                  if (Text != value)
                  { 
                      SetValue(TextProperty, value);
                  }
              }
          }
      
          public static readonly DependencyProperty TextProperty =
              DependencyProperty.Register("Text", typeof(string), typeof(TemplateEditor),new PropertyMetadata(null,Text_PropertyChanged));
      
          public TemplateEditor()
          {
              InitializeComponent();
      
              textBox.TextChanged += new TextChangedEventHandler(textBox_TextChanged);
          }
      
          private static void Text_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
          {
              ((TemplateEditor)source).textBox.Text = (string)e.NewValue;
          }
      
      
          void textBox_TextChanged(object sender, TextChangedEventArgs e)
          {
              Text = textBox.Text;
          }
      }
      

      感谢您的宝贵时间,并表示歉意。 :)

      【讨论】:

        猜你喜欢
        • 2011-01-05
        • 1970-01-01
        • 2012-09-17
        • 2012-02-17
        • 1970-01-01
        • 2017-01-28
        • 1970-01-01
        • 2012-04-18
        • 1970-01-01
        相关资源
        最近更新 更多