简体   繁体   English

如何从C#修改XAML DataTemplate中定义的元素的值?

[英]How can I modify from C# the value of an element which is define in a XAML DataTemplate?

I created a "WPF Application Project" in Visual Studio 2013. 我在Visual Studio 2013中创建了“ WPF应用程序项目”。

I opened the "MainWindow.xaml" file and I wrote the following XAML code: 我打开“ 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"
    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>

In this XAML file I created a "DataTemplate" which corresponds to the key "AlphaDataTemplate". 在此XAML文件中,我创建了一个与键“ AlphaDataTemplate”相对应的“ DataTemplate”。 The DataTmplate contains just one label with the name "LabelInDataTemplate" where I have hardcoded the "Good morning!" DataTmplate仅包含一个名称为“ LabelInDataTemplate”的标签,在其中我已硬编码了“早上好!”。 string in the "Content" attribute of the label. 标签的“内容”属性中的字符串。

Then I use created a "ContentPresenter" with the name "MyContentPresenter" and I pass as content the "DataTemplate" I previously created (AlphaDataTemplate). 然后,我使用名称为“ MyContentPresenter”的“ ContentPresenter”进行创建,并将先前创建的“ DataTemplate”(AlphaDataTemplate)作为内容传递。

As next step, I created a "Button" with the name "MyButton" and I have set a "Click" event called "MyButton_OnClick" 下一步,我创建了一个名为“ MyButton”的“ Button”,并设置了一个名为“ MyButton_OnClick”的“ Click”事件。

So far so good...! 到现在为止还挺好...!

The question comes now and actually in C# in the code behind file "MainWindow.xaml.cs". 现在,实际上是在C#文件“ MainWindow.xaml.cs”后面的代码中出现了问题。 See the code below: 请参见下面的代码:

using System.Windows;

namespace TestProject { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); 名称空间TestProject {公共局部类MainWindow:Window {public MainWindow(){InitializeComponent();

    }
    private void MyButton_OnClick(object sender, RoutedEventArgs e)
    {
        LabelInDataTemplate.Content = "Bye!";   // <-- Tha does not work.
    }
}

} }

In this C# code behind file you can see the definition of the "Click" (MyButton_OnClick) event of the Button (MyButton) which appears in XAML. 在此C#代码隐藏文件中,您可以看到XAML中出现的按钮(MyButton)的“ Click”(MyButton_OnClick)事件的定义。

What I am trying to do in this "Click" event, is to change the value of the "Content" of the "Label" (LabelInDataTemplate) which is in the DataTemplate (AlphaDataTemplate). 我要在此“单击”事件中尝试做的是更改DataTemplate(AlphaDataTemplate)中“ Label”(LabelInDataTemplate)的“ Content”的值。

Unfortunately, that does not work. 不幸的是,这行不通。

I cannot actually access the "Name" (LabelInDataTemplate) of the "Label", because it is contained in the "DataTemplate" (AlphaDataTemplate) 我实际上无法访问“标签”的“名称”(LabelInDataTemplate),因为它包含在“ DataTemplate”(AlphaDataTemplate)中

If anyone has any idea, how could I modify from C# the value of an element which is define in a XAML DataTemplate, please give me feedback. 如果有人有任何想法,我该如何从C#修改XAML DataTemplate中定义的元素的值,请给我反馈。 I would really appreciate it. 我真的很感激。

Thank you in advance. 先感谢您。

Please learn MVVM and use proper DataBinding for this purpose. 请学习MVVM并为此使用适当的DataBinding。 For sake of solving this problem: 为了解决这个问题:

  1. Implement INotifyPropertyChanged interface on your Window class and Define string property like below 在Window类上实现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)); } } 
  2. In your xaml bind the ContentPresenter and update your DataTemplate label like 在您的xaml中,绑定ContentPresenter并更新您的DataTemplate标签,例如

     <ContentPresenter Name="MyContentPresenter" Content = "{Binding ContentMsg}" ContentTemplate="{StaticResource AlphaDataTemplate}" /> <DataTemplate x:Key="AlphaDataTemplate"> <Label Name="LabelInDataTemplate" Content="{Binding}" /> 

  3. Now in click handler (I would use Commands here), set ContentMsg to whatever you want 现在在点击处理程序中(我将在此处使用命令),将ContentMsg设置为所需的任何值

      private void MyButton_OnClick(object sender, RoutedEventArgs e) { ContentMsg = "Bye!"; } 

I strongly oppose your method of changing the content of label via DataTemplate, However your requirement is possible, but very subtle. 我强烈反对您通过DataTemplate更改标签内容的方法,但是您的要求是可能的,但是非常微妙。

Code

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";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#/ XAML:从DataGridView中的DataTemplate文本框中获取文本框值 - C#/XAML: Get Textbox value from DataTemplate Textbox in DataGridView 如何在XAML c#中绑定来自字典的值(即struct)? - How to bind value from Dictionary, which is struct, in XAML c#? 如何从DataTemplate获取XAML Control的值 - How to get value of XAML Control from DataTemplate 使用 C# 如何从 xaml 返回切换开关的值 - Using C# how can I return the value of a toggle Switch from xaml C# - 如何从xaml resourcedictionary格式的字符串中获取密钥和值? - C# - How can I get the key and value from strings of xaml resourcedictionary format? 如何在xaml中定义和使用资源,以便它们可以在C#中使用 - How to define and use resources in xaml so they can be used in C# 设置来自C#(XAML)的数据模板内部的textBlock文本 - Set textBlock text that is inside a datatemplate from C# (XAML) C#WPF MVVM-如何从ItemsControl / DataTemplate访问特定项目? - C# WPF MVVM - How can I access a specific item from ItemsControl/DataTemplate? 如何修改另一个类(C#-WPF)中的XAML元素 - How can i modify XAML elements from another class (C#-WPF) 如何在XAML中使GridViewItem成为可拖动对象而不是DataTemplate? - How can I make a GridViewItem as a draggable object instead of a DataTemplate in XAML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM