WPF 更改 UserControl 中子项的值
WPF change value of a child inside UserControl
我需要更改 CustomControl 中 Control 的 MainWindow 的值。
所以假设我想更改 Labels Content inside UserControl MyControl 来自 MainWindow.xaml.
示例:
<UserControl x:Class="XXXXX.MyUserControl"
.
.
.
>
<Grid>
<Label x:Name="TestLabel"/>
</Grid>
</UserControl>
在 MainWindow.xaml 中:
<MyUserControl x:Name="TestControl" />
现在如何从 MainWindow.xaml 中的 Xaml Designer 访问 Label.Content?
我没有在那里找到任何东西,所以希望有人知道该怎么做。
非常感谢
将以下内容添加到您的 UserControl
<UserControl x:Class="XXXXX.MyUserControl"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
.
.
>
让用户控件实现 INotify属性已更改
像这样向用户控件添加一个属性
Private _LabelText As String
Public Property LabelText() As String
Get
Return _LabelText
End Get
Set(ByVal value As String)
_LabelText = value
OnPropertyChanged("LabelText")
End Set
End Property
更新要绑定的标签 属性
<Label x:Name="TestLabel" Content="{Binding Path=LabelText}"/>
然后在您的主窗口中,您可以相应地更改 属性
<MyUserControl x:Name="TestControl" LabelText="Testing" />
那你后面的代码也可以引用那个属性
在您的 UserControl 中公开自定义 属性,如下所示
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
var dpd = DependencyPropertyDescriptor.FromProperty(LabelContentProperty, typeof(MyUserControl));
dpd.AddValueChanged(this, (sender, args) =>
{
_label.Content = this.LabelContent;
});
}
public static readonly DependencyProperty LabelContentProperty = DependencyProperty.Register("LabelContent", typeof(string), typeof(MyUserControl));
public string LabelContent
{
get
{
return GetValue(LabelContentProperty) as string;
}
set
{
SetValue(LabelContentProperty, value);
}
}
}
在主窗口xaml中
<MyUserControl x:Name="TestControl" LabelContent="Some Content"/>
我需要更改 CustomControl 中 Control 的 MainWindow 的值。 所以假设我想更改 Labels Content inside UserControl MyControl 来自 MainWindow.xaml.
示例:
<UserControl x:Class="XXXXX.MyUserControl"
.
.
.
>
<Grid>
<Label x:Name="TestLabel"/>
</Grid>
</UserControl>
在 MainWindow.xaml 中:
<MyUserControl x:Name="TestControl" />
现在如何从 MainWindow.xaml 中的 Xaml Designer 访问 Label.Content?
我没有在那里找到任何东西,所以希望有人知道该怎么做。
非常感谢
将以下内容添加到您的 UserControl
<UserControl x:Class="XXXXX.MyUserControl"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
.
.
>
让用户控件实现 INotify属性已更改
像这样向用户控件添加一个属性
Private _LabelText As String
Public Property LabelText() As String
Get
Return _LabelText
End Get
Set(ByVal value As String)
_LabelText = value
OnPropertyChanged("LabelText")
End Set
End Property
更新要绑定的标签 属性
<Label x:Name="TestLabel" Content="{Binding Path=LabelText}"/>
然后在您的主窗口中,您可以相应地更改 属性
<MyUserControl x:Name="TestControl" LabelText="Testing" />
那你后面的代码也可以引用那个属性
在您的 UserControl 中公开自定义 属性,如下所示
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
var dpd = DependencyPropertyDescriptor.FromProperty(LabelContentProperty, typeof(MyUserControl));
dpd.AddValueChanged(this, (sender, args) =>
{
_label.Content = this.LabelContent;
});
}
public static readonly DependencyProperty LabelContentProperty = DependencyProperty.Register("LabelContent", typeof(string), typeof(MyUserControl));
public string LabelContent
{
get
{
return GetValue(LabelContentProperty) as string;
}
set
{
SetValue(LabelContentProperty, value);
}
}
}
在主窗口xaml中
<MyUserControl x:Name="TestControl" LabelContent="Some Content"/>