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
为了让它工作,我删除了你的控件的DataContext设置,为每个控件添加一个x:name,并使用[name] .SetBinding方法在控件的构造函数中设置绑定。
我在ctor中执行了绑定,因为我无法找出一种将xaml中的声明绑定的Source属性设置为Self的方法。即{Binding SelectedText,Mode = TwoWay,Source = [Self here some how]}。我没有尝试使用relativeSource = {relativeSource Self}而没有喜悦。
注意:这一切都是SL3。
总结以上是内存溢出为你收集整理的Silverlight UserControl自定义属性绑定全部内容,希望文章能够帮你解决Silverlight UserControl自定义属性绑定所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)