【问题标题】:Custom usercontrol property binding failure silverlight自定义用户控件属性绑定失败 silverlight
【发布时间】:2012-02-18 15:51:19
【问题描述】:

我有一个带有DataContext="{Binding RelativeSource={RelativeSource self}}" 的自定义用户控件

在后面的代码中,我创建了一个依赖属性,例如:

    public static DependencyProperty ElementNameProperty = DependencyProperty.Register("ElementName",
        typeof(string),
        typeof(ElementControl),
        new PropertyMetadata(new PropertyChangedCallback((s, e) => { new Base().OnPropertyChanged("ElementName"); })));

    public string ElementName
    {
        get
        {
            return (string)base.GetValue(ElementNameProperty);
        }
        set
        {
            base.SetValue(ElementNameProperty, value);

        }
    }

现在,当我尝试在我的 mainpage.xaml 中使用此用户控件并使用以下绑定:<test.TestControl ElementName="{Binding name}" /> 时,它一直在我的自定义用户控件中搜索“名称”属性,而不是它应该来自哪里?

我做错了什么?

【问题讨论】:

    标签: c# silverlight binding custom-controls dependency-properties


    【解决方案1】:

    它在那里搜索是因为您在最顶层为您的用户控件设置了DataContext。您需要做的是摆脱用户控件中与 self 的相对绑定,并在绑定中指定 ElementName (在用户控件内部)。顺便说一句,您可能不需要 OnPropertyChangedPropertyChangedCallback 中,因为 DependencyProperties 本质上会通知值的变化。

    【讨论】:

    • 它并没有真正解决问题。删除数据上下文使得父控件的绑定转移到子控件。因此,我必须将绑定名称更改为控件中的名称。
    【解决方案2】:

    我最终以这种方式解决了它。不是我想要的方式,但它(在我看来)是一个非常简洁的解决方案。

    CustomUserControl.xaml

    <UserControl x:Class="TestApp.Controls.CustomUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             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"
             Width="75"
             Height="75">
      <Canvas x:Name="LayoutRoot"
          Background="Black">
        <StackPanel Orientation="Vertical">
          <Image x:Name="UCImage"
             Width="50"
             Height="50"
             HorizontalAlignment="Center" />
          <TextBlock x:Name="UCText"
                 HorizontalAlignment="Center" />
        </StackPanel>
      </Canvas>
    </UserControl>
    

    CustomUserControl.xaml.cs

    public partial class ElementControl : UserControl
    {
        #region DependencyProperty ElementNameProperty
        public static DependencyProperty ElementNameProperty = DependencyProperty.Register("ElementName",
            typeof(string),
            typeof(ElementControl),
            new PropertyMetadata(new PropertyChangedCallback((s, e) => 
        { 
        //See Here
        ((ElementControl)s).UCText.Text = e.NewValue as string; 
        })));
    
        public string ElementName
        {
            get
            {
                return (string)base.GetValue(ElementNameProperty);
            }
            set
            {
                base.SetValue(ElementNameProperty, value);
            }
        }
        #endregion
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-20
      • 2011-10-13
      • 2011-03-24
      • 2010-12-04
      • 1970-01-01
      • 1970-01-01
      • 2018-03-11
      • 1970-01-01
      相关资源
      最近更新 更多