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

Silverlight UserControl自定义属性绑定

在Silverlight UserControls中实现自定义属性的正确方法是什么?

Silverlight中的每个“Page”在技术上都是一个UserControl(它们来源于UserControl类)。当我在这里说UserControl时,我的意思是在许多不同的场景(类似于ASP.NET UserControl)的许多不同页面中使用的Custom UserControl。

我希望Custom UserControl支持绑定,而不是依赖它所绑定的属性名称,始终是相同的。相反,我希望UserControl本身具有UserControl中的Controls控件绑定的属性,UserControl之外的viewmodel也绑定到。 (请参见下面的例子)

在UserControl中的绑定工作,MainPage中的绑定工作,我在MainPage和UserControl之间建立的绑定不起作用。具体如下:

<myUserControls:MyCustomUserControl x:Name="MyCustomControl2" 
    SelectedText="{Binding MainPageSelectedText,Mode=TwoWay}" 
    Width="200" Height="50" />

示例输出

MainPage.xaml中

<UserControl x:Class="SilverlightCustomUserControl.MainPage"
    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" 
    xmlns:myUserControls="clr-namespace:SilverlightCustomUserControl"
    mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">
  <Canvas x:Name="LayoutRoot">
    <StackPanel Orientation="Vertical">
      <TextBlock Text="UserControl Binding:" Width="200"></TextBlock>
      <myUserControls:MyCustomUserControl x:Name="MyCustomControl2" SelectedText="{Binding MainPageSelectedText,Mode=TwoWay}" Width="200" Height="50" />
      <TextBlock Text="MainPage Binding:" Width="200"></TextBlock>
      <TextBox Text="{Binding MainPageSelectedText,Mode=TwoWay}" Width="200"></TextBox>
      <Border BorderBrush="Black" BorderThickness="1">
        <TextBlock Text="{Binding MainPageSelectedText}" Width="200" Height="24"></TextBlock>
      </Border>
    </StackPanel>
  </Canvas>
</UserControl>

MainPage.xaml.cs中

namespace SilverlightCustomUserControl
{
 public partial class MainPage : UserControl,INotifyPropertyChanged
 {
  //NOTE: would probably be in a viewmodel
  public string MainPageSelectedText
  {
   get { return _MainPageSelectedText; }
   set
   {
    string myValue = value ?? String.Empty;
    if (_MainPageSelectedText != myValue)
    {
     _MainPageSelectedText = value;
     OnPropertyChanged("MainPageSelectedText");
    }
   }
  }
  private string _MainPageSelectedText;


  public MainPage()
  {
   InitializeComponent();
  }


  #region INotifyPropertyChanged Members

  public event PropertyChangedEventHandler PropertyChanged;

  protected virtual void OnPropertyChanged(string name)
  {
   PropertyChangedEventHandler ph = this.PropertyChanged;

   if (ph != null)
    ph(this,new PropertyChangedEventArgs(name));
  }

  #endregion
 }
}

MyCustomUserControl.xaml

<UserControl
   x:Class="SilverlightCustomUserControl.MyCustomUserControl" 
   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"
   DataContext="{Binding RelativeSource={RelativeSource Self}}">
  <Grid>
    <StackPanel>
      <TextBox Text="{Binding SelectedText,Mode=TwoWay}" />
      <Border BorderBrush="Black" BorderThickness="1">
        <TextBlock Text="{Binding SelectedText}" Height="24"></TextBlock>
      </Border>
    </StackPanel>
  </Grid>
</UserControl>

MyCustomUserControl.xaml.cs

namespace SilverlightCustomUserControl
{
 public partial class MyCustomUserControl : UserControl
 {
  public string SelectedText
  {
   get { return (string)GetValue(SelectedTextProperty); }
   set { SetValue(SelectedTextProperty,value); }
  }

  public static readonly DependencyProperty SelectedTextProperty =
    DependencyProperty.Register("SelectedText",typeof(string),typeof(MyCustomUserControl),new PropertyMetadata("",SelectedText_PropertyChangedCallback));


  public MyCustomUserControl()
  {
   InitializeComponent();
  }

  private static void SelectedText_PropertyChangedCallback(DependencyObject d,DependencyPropertyChangedEventArgs e)
  {
   //empty
  }
 }
}

参考(我怎么这么远):

使用DependencyPropertys:
http://geekswithblogs.net/thibbard/archive/2008/04/22/wpf-custom-control-dependency-property-gotcha.aspx

使用DependencyPropertys,将x:Name添加到UserControl中 – 使用ElementName添加绑定,再次在PropertyChangedCallback方法中设置Custom属性
Setting Custom Properties in UserControl via DataBinding

不要使用自定义属性,依赖于底层数据标签名(我不喜欢这个解决方案):
wpf trouble using dependency properties in a UserControl

解决方法

我理解它是因为您的控件没有从maim页面接收到新值,因为您正在设置控件的DataContext。如果没有,那么控件的DataContext将从它的父代继承,这种情况下的主页面

为了让它工作,我删除了你的控件的DataContext设置,为每个控件添加一个x:Name,并使用[name] .SetBinding方法在控件的构造函数中设置绑定。

我在ctor中执行了绑定,因为我无法找出一种将xaml中的声明绑定的Source属性设置为Self的方法。即{Binding SelectedText,Mode = TwoWay,Source = [Self here some how]}。我没有尝试使用RelativeSource = {RelativeSource Self}而没有喜悦。

注意:这一切都是SL3。

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

相关推荐