如何从C#修改XAML数据模板中定义的元素的值?
我在Visual Studio 2013中创建了一个“WPF应用程序项目” 我打开了“MainWindow.xaml”文件,并编写了以下xaml代码:如何从C#修改XAML数据模板中定义的元素的值?,c#,wpf,xaml,C#,Wpf,Xaml,我在Visual Studio 2013中创建了一个“WPF应用程序项目” 我打开了“MainWindow.xaml”文件,并编写了以下xaml代码: <Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
<Window x:Class="TestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate x:Key="AlphaDataTemplate">
<Label
Name="LabelInDataTemplate"
Content="Good morning!" />
</DataTemplate>
</Window.Resources>
<Grid>
<ContentPresenter
Name="MyContentPresenter"
ContentTemplate="{StaticResource AlphaDataTemplate}" />
<Button
Name="MyButton"
Click="MyButton_OnClick"
Content="Change the content of the Label in the DataTemplate"
Width="320"
Height="30" />
</Grid>
命名空间测试项目
{
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件()
}
private void MyButton_OnClick(对象发送方,路由目标)
{
LabelInDataTemplate.Content=“Bye!”;//请学习MVVM并为此使用适当的数据绑定。为了解决此问题:
在窗口类上实现INotifyPropertyChanged接口并定义字符串属性,如下所示
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public string _contentMsg;
public string ContentMsg
{
get { return _contentMsg; }
set
{
_contentMsg = value;
RaisePropertyChanged("ContentMsg");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propName)
{
if(PropertyChanged !=null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
在xaml中绑定ContentPresenter并更新DataTemplate标签,如
<ContentPresenter
Name="MyContentPresenter"
Content = "{Binding ContentMsg}"
ContentTemplate="{StaticResource AlphaDataTemplate}" />
<DataTemplate x:Key="AlphaDataTemplate">
<Label
Name="LabelInDataTemplate"
Content="{Binding}" />
我强烈反对您通过DataTemplate更改标签内容的方法,但是您的要求是可能的,但非常微妙
代码
您发布的解决方案非常有效!非常感谢!:)非常感谢您的反馈。您的解决方案符合开发的最佳实践,但我想我必须同时使用您的解决方案和Arun Selve Kumar提供的上述解决方案。这是因为在我开发的代码中,我需要更改“可见性”从“Visibility.Hidden”到“Visibility.Visible”的“矩形”属性,反之亦然。这很好。但是您也可以使用转换器以MVVM方式切换可见性。。
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
public string _contentMsg;
public string ContentMsg
{
get { return _contentMsg; }
set
{
_contentMsg = value;
RaisePropertyChanged("ContentMsg");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propName)
{
if(PropertyChanged !=null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
<ContentPresenter
Name="MyContentPresenter"
Content = "{Binding ContentMsg}"
ContentTemplate="{StaticResource AlphaDataTemplate}" />
<DataTemplate x:Key="AlphaDataTemplate">
<Label
Name="LabelInDataTemplate"
Content="{Binding}" />
private void MyButton_OnClick(object sender, RoutedEventArgs e)
{
ContentMsg = "Bye!";
}
private void MyButton_OnClick(object sender, RoutedEventArgs e)
{
var alphaDataTemplate = this.Resources["AlphaDataTemplate"] as DataTemplate;
var label = alphaDataTemplate.FindName("LabelInDataTemplate", MyContentPresenter) as Label;
label.Content = "It Works";
}