我创建了一个UserControl,如下所示:
<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>
Run Code Online (Sandbox Code Playgroud)
现在,在我的主页面中,我想将用户控件中出现的文本从"Sample"更改为Hello World.所以,我在mainpage.xaml中这样做了
<local:MyControl x:Name="MyControl" Margin="100,50 0,0"></local:MyControl>
Run Code Online (Sandbox Code Playgroud)
在我尝试引用MyControl的mainpage.xaml.cpp中,它似乎无法识别:
MainPage::MainPage(){MyControl->Text = "Hello World";}
Run Code Online (Sandbox Code Playgroud)
任何的想法 ?
sha*_*ren 13
详细说明@Steven你的答案,在你的UserControl中定义一个DependencyProperty.定义DependencyProperty允许更改通知以触发对控件的更新.
在UserControl的代码隐藏中,您可以添加依赖项属性.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这会暴露DependencyProperty您可以在UserControl的XAML中绑定的公共.
<UserControl>
<TextBlock Text="{Binding Path=TextBlockText}" />
</UserControl>
Run Code Online (Sandbox Code Playgroud)
现在,您需要一种从Window控件设置该属性的方法.我将详细介绍三种方法:
1.)由于TextBlockText属性在UserControl上公开,我们可以直接在XAML中设置它,如:
<Window x:Class="WpfApplication2.MainWindow"
xmlns:local="clr-namespace:WpfApplication2">
<local:MyUserControl TextBlockText="Text that you want to set.">
</local:MyUserControl>
</Window>
Run Code Online (Sandbox Code Playgroud)
2.)如果我们给UserControl一个名字,我们可以改变Window代码隐藏中的属性:
<Window x:Class="WpfApplication2.MainWindow"
xmlns:local="clr-namespace:WpfApplication2">
<local:MyUserControl Name="CoolUserControl">
</local:MyUserControl>
</Window>
Run Code Online (Sandbox Code Playgroud)
-
CoolUserControl.TextBlockText = "Text that you want to set.";
Run Code Online (Sandbox Code Playgroud)
3.)或者最后你可以DependencyProperty在你Window的代码隐藏中创建另一个并将它绑定到UserControl的依赖属性.这样,每当您更新属性时Window代码中的值UserControl依赖项属性也会更改.这是最好的选择@Steven你之前说过,因为你的代码背后不需要知道任何控件.
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.";
}
}
Run Code Online (Sandbox Code Playgroud)
并绑定到我们DependencyProperty在Window XAML中的新功能:
<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>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!