Cách thích hợp để triển khai Thuộc tính tùy chỉnh trong Silverlight UserControls là gì?Silverlight UserControl Custom Property Binding
Mỗi "Trang" trong Silverlight về mặt kỹ thuật là một UserControl (chúng được bắt nguồn từ lớp UserControl). Khi tôi nói UserControl ở đây, tôi có nghĩa là một UserControl tùy chỉnh sẽ được sử dụng bên trong nhiều trang khác nhau trong nhiều kịch bản khác nhau (tương tự như một UserControl ASP.NET).
Tôi muốn Custom UserControl hỗ trợ Binding và không dựa vào Tên của thuộc tính mà nó ràng buộc, luôn giống nhau. Thay vào đó, tôi muốn UserControl chính nó có một thuộc tính mà các điều khiển bên trong UserControl liên kết với, và các ViewModels bên ngoài UserControl cũng liên kết với. (vui lòng xem ví dụ bên dưới)
Ràng buộc trong công việc UserControl, Ràng buộc trong các công trình MainPage, Ràng buộc tôi thiết lập giữa MainPage và UserControl không hoạt động. Cụ thể dòng này:
<myUserControls:MyCustomUserControl x:Name="MyCustomControl2"
SelectedText="{Binding MainPageSelectedText, Mode=TwoWay}"
Width="200" Height="50" />
dụ đầu ra:
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
01.<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
}
}
}
Tài liệu tham khảo (làm thế nào tôi có này đến nay):
DependencyPropertys sử dụng: http://geekswithblogs.net/thibbard/archive/2008/04/22/wpf-custom-control-dependency-property-gotcha.aspx
DependencyPropertys sử dụng, thêm x: Đặt tên cho usercontrol của bạn - thêm Ràng buộc với ElementName, đặt thuộc tính tùy chỉnh một lần nữa trong phương thức PropertyChangedCallback:
không sử dụng các thuộc tính tùy chỉnh, dựa vào tên DataContext cơ bản (Tôi không thích giải pháp này): wpf trouble using dependency properties in a UserControl
Nếu bạn vẫn đang tìm kiếm câu trả lời, bạn có thể tham khảo bài viết này [Liên kết tài sản tùy chỉnh Silverlight UserControl] (http://www.dotnetspark.com/kb/4664-silverlight-usercontrol-custom-property.aspx) –