【问题标题】:Dependency Property ListBox依赖属性列表框
【发布时间】:2010-04-19 18:03:38
【问题描述】:

我想使用依赖属性,以便我的标签显示在列表框中选择的值。这只是为了更清楚地了解依赖属性的工作原理。

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WPFToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
    xmlns:local="clr-namespace:WpfApplication1"
    x:Name="MyWindow"
    Height="200"
    Width="300">
       <StackPanel>
        <ListBox x:Name="lbColor"
                   Width="248"
                   Height="56"
                   ItemsSource="{Binding TestColor}"/>
       <StackPanel>
      <Label Content="{Binding Path=Test, ElementName=lbColor}" />
    </StackPanel>
  </StackPanel>
</Window>

代码隐藏,

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {

        public ObservableCollection<string> TestColor { get; set; }

        public String Test
        {
            get { return (String)GetValue(TestProperty); }
            set { SetValue(TestProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Title.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TestProperty =
            DependencyProperty.Register("Test", typeof(String), typeof(ListBox), new UIPropertyMetadata("Test1"));



        public Window1()
        {
            InitializeComponent();
            TestColor = new ObservableCollection<string>();
            DataContext = this;
            TestColor.Add("Red");
            TestColor.Add("Orange");
            TestColor.Add("Yellow");
            TestColor.Add("Green");
            TestColor.Add("Blue");
        }
    }
}

谁能向我解释我将如何使用依赖属性来完成此任务?不知何故,我对依赖属性的概念感到非常困惑,我只是想看看一个可行的例子。

【问题讨论】:

  • 关于您使用依赖属性完成的工作,请看这里:stackoverflow.com/questions/1723756/why-dependency-properties/…
  • 我明白有很多例子展示了 DP 和与为什么使用它相关的文档,但我希望看到一些有用的东西,让我了解 DP 在什么步骤中所做的事情.就像它是如何调用的一样,在什么时候设置值以及正在设置的值是什么等等......

标签: c# wpf


【解决方案1】:

您需要让您的列表框“选择”当前文本:

<StackPanel>
    <!-- Add selected item binding -->
    <ListBox 
         x:Name="lbColor" Width="248" Height="56" 
         ItemsSource="{Binding TestColor}"
         SelectedItem="{Binding Test}"
    />

    <StackPanel>
        <!-- No need for elementname - just use Test on the DataContext -->
        <Label Content="{Binding Path=Test}" />
    </StackPanel>
</StackPanel>

【讨论】:

  • 在我将 typeof 依赖属性从 ListBox 更改为 Window1 后,上面的代码工作。先生,你能解释一下技术上到底发生了什么吗?我的意思是,在我单击列表框值后是否设置了 DP 的值。每当我想在 XAML 中使用 DP 时,我是否必须将 DP 的值设置为某个源,就像我们在这里所做的那样??
  • 哦,是的 - 我错过了不是这样设置的。您需要将 DependencyProperty 类型设置为您的类 - 因为您在类上定义 DP,而不是在列表框上。现在,发生的事情是,当您选择一个新项目(在列表框中)时,列表框通过绑定回您的 Window1 类来设置“SelectedItem”值。标签反映了属性的新值,因为它是一个 DP,并且它会自行更新。
【解决方案2】:

我喜欢将数据绑定视为老大哥。 Binding 系统将自己设置为监视其所有各种已注册的 Binding,并且当发生适当的条件(例如,FocusLost 或 PropertyChanged)时,Binding 系统将源值复制到目标。对于 TwoWay 或 OneWayToSource 绑定,如果条件正确,Binding 系统甚至会从目标复制到源。

目标必须是 DependencyProperty,因为这是一种知道如何依赖其他值的特殊属性。 XAML {Binding} 在幕后所做的是创建一个新的 BindingExpression,然后调用 BindingOperations.SetBinding,后者向 Binding System 注册一个特定的 BindingExpression,因此它知道如何监视和执行更新。

这样做的好处是目标和源都不需要负责编写代码来显式更新对方。如果我有知道如何提供颜色列表的代码,我为什么要关心颜色如何呈现给用户?因为绑定系统负责绑定,所以使用我的颜色的目标是列表框、树视图或任何其他知道如何处理项目列表的对象对我来说并不重要。我可以专注于我关心的事情(我正在公开的公共接口),而不必关心将该接口与其他东西粘合的混乱细节。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 2012-07-13
    相关资源
    最近更新 更多