2012-04-03 34 views
7

tôi đã tạo một UserControl như sau:yếu tố Truy cập của một User Control

<UserControl 
x:Class="MySample.MyControl" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:MySample" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" 
d:DesignHeight="300" 
d:DesignWidth="400"> 

<Canvas> 

    <Ellipse Width="150" Height="150"/> 

    <TextBlock>Sample</TextBlock> 

</Canvas> 

Bây giờ, từ trang chính của tôi, tôi sẽ muốn thay đổi các văn bản xuất hiện trong User Control của tôi từ "mẫu" đến Hello World. Vì vậy, tôi đã làm điều này trong mainpage.xaml tôi

<local:MyControl x:Name="MyControl" Margin="100,50 0,0"></local:MyControl> 

Và trong mainpage.xaml.cpp khi tôi cố gắng để tham khảo MyControl, nó dường như không nhận biết được:

MainPage::MainPage(){MyControl->Text = "Hello World";} 

Bất kỳ ý tưởng?

Trả lời

10

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!

1

Trong chế độ xem chương trình, cách tốt nhất để thực hiện việc này là sử dụng ràng buộc dữ liệu thay vì đặt giá trị ở mã phía sau. Để giải quyết vấn đề này, cách đơn giản nhất là đăng ký thuộc tính phụ thuộc của UserControl ràng buộc giá trị này vào TextBlock và sau đó đặt giá trị trong MainPage.

+0

bạn có thể đưa ra một ví dụ nhỏ về cách thực hiện không? –

+0

Đọc nội dung này: http: //www.codeproject.com/Articles/224230/Khám phá-sử dụng-of-Dependency-Properties-in-User –

+0

Có một cái nhìn tại databinding này [ví dụ] (http://code.msdn.microsoft.com/windowsapps/Data-Binding- 7b1d67b5) –

Các vấn đề liên quan