Aar*_*man 16 silverlight binding user-controls
在Silverlight UserControls中实现自定义属性的正确方法是什么?
Silverlight中的每个"页面"在技术上都是UserControl(它们派生自UserControl类).当我在这里说UserControl时,我指的是一个自定义UserControl,它将在许多不同场景中的许多不同页面中使用(类似于ASP.NET UserControl).
我希望Custom UserControl支持Binding而不依赖于它绑定的Property的名称,始终保持相同.相反,我希望UserControl本身具有UserControl内的控件绑定的属性,并且UserControl外部的ViewModels也绑定到.(请看下面的例子)
UserControl中的绑定工作,MainPage中的绑定工作,我在MainPage和UserControl之间设置的绑定不起作用.特别是这一行:
<myUserControls:MyCustomUserControl x:Name="MyCustomControl2"
SelectedText="{Binding MainPageSelectedText, Mode=TwoWay}"
Width="200" Height="50" />
Run Code Online (Sandbox Code Playgroud)
示例输出:

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>
Run Code Online (Sandbox Code Playgroud)
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
}
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
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
}
}
}
Run Code Online (Sandbox Code Playgroud)
参考文献(我是如何做到这一点):
使用DependencyPropertys:http: //geekswithblogs.net/thibbard/archive/2008/04/22/wpf-custom-control-dependency-property-gotcha.aspx
使用DependencyPropertys,将x:Name添加到UserControl - 使用ElementName添加Binding,再次在PropertyChangedCallback方法中设置Custom属性: 通过DataBinding在UserControl中设置自定义属性
不使用自定义属性,依赖底层datacontext名称(我不喜欢这个解决方案): wpf在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.
| 归档时间: |
|
| 查看次数: |
18679 次 |
| 最近记录: |