Chi tiết ra @Steven Câu trả lời của bạn, trong UserControl của bạn xác định một DependencyProperty
. Xác định DependencyProperty
cho phép thông báo thay đổi kích hoạt cập nhật cho các điều khiển của bạn.
Trong mã phía sau UserControl của bạn, bạn có thể thêm thuộc tính phụ thuộc.
public partial class MyUserControl : UserControl
{
public string TextBlockText
{
get { return (string)GetValue(TextBlockTextProperty); }
set { SetValue(TextBlockTextProperty, value); }
}
public static readonly DependencyProperty TextBlockTextProperty =
DependencyProperty.Register("TextBlockText", typeof(string), typeof(MyUserControl), new UIPropertyMetadata(""));
public MyUserControl()
{
InitializeComponent();
DataContext = this;
}
}
Điều này sẽ hiển thị công khai DependencyProperty
mà bạn có thể liên kết trong XAML của UserControl.
<UserControl>
<TextBlock Text="{Binding Path=TextBlockText}" />
</UserControl>
Bây giờ bạn cần có cách để đặt thuộc tính đó từ điều khiển Cửa sổ. Tôi sẽ chi tiết ba cách mà bạn có thể làm điều này:
1.) Kể từ khi sở hữu TextBlockText
được tiếp xúc trên UserControl chúng ta có thể thiết lập nó trực tiếp trong XAML như:
<Window x:Class="WpfApplication2.MainWindow"
xmlns:local="clr-namespace:WpfApplication2">
<local:MyUserControl TextBlockText="Text that you want to set.">
</local:MyUserControl>
</Window>
2.) Nếu chúng tôi cung cấp cho các UserControl một tên chúng ta có thể thay đổi thuộc tính trong cửa sổ code-behind:
<Window x:Class="WpfApplication2.MainWindow"
xmlns:local="clr-namespace:WpfApplication2">
<local:MyUserControl Name="CoolUserControl">
</local:MyUserControl>
</Window>
-
CoolUserControl.TextBlockText = "Text that you want to set.";
3.) Hoặc cuối cùng bạn có thể tạo một mã khác DependencyProperty
trong mã của bạn Window
và liên kết nó với thuộc tính phụ thuộc của UserControl
. Bằng cách này bất cứ khi nào bạn cập nhật thuộc tính, giá trị trong mã Window
thuộc tính phụ thuộc UserControl
cũng sẽ thay đổi. Đây là lựa chọn thích hợp hơn như @Steven Bạn đã nói trước đây, vì mã của bạn phía sau không cần biết về bất kỳ điều khiển nào.
public partial class MainWindow : Window
{
public string UserControlText
{
get { return (string)GetValue(UserControlTextProperty); }
set { SetValue(UserControlTextProperty, value); }
}
public static readonly DependencyProperty UserControlTextProperty =
DependencyProperty.Register("UserControlText", typeof(string), typeof(MainWindow), new UIPropertyMetadata(""));
public MainWindow()
{
InitializeComponent();
DataContext = this;
UserControlText = "Text that you want to set.";
}
}
Và để ràng buộc mới của chúng tôi DependencyProperty
trong XAML Window:
<Window x:Class="WpfApplication2.MainWindow"
xmlns:local="clr-namespace:WpfApplication2">
<local:MyUserControl TextBlockText="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}, Path=UserControlText}"></local:MyUserControl>
</Window>
Hope this helps!
bạn có thể đưa ra một ví dụ nhỏ về cách thực hiện không? –
Đọc nội dung này: http: //www.codeproject.com/Articles/224230/Khám phá-sử dụng-of-Dependency-Properties-in-User –
Có một cái nhìn tại databinding này [ví dụ] (http://code.msdn.microsoft.com/windowsapps/Data-Binding- 7b1d67b5) –