【问题标题】:How to get value of XAML Control from DataTemplate如何从 DataTemplate 获取 XAML 控件的值
【发布时间】:2017-07-21 08:19:10
【问题描述】:

我尝试在我的 XAML 数据模板中定义的按钮单击事件上从 TextBox 中获取值,如下所示:

        <GridView.ItemTemplate>
            <DataTemplate x:DataType="data:Ausstattung">
                <Grid Height="40" Width="Auto" Background="LightSlateGray" >

                    <TextBlock Grid.Column="0" Foreground="White" FontSize="14" Text="{x:Bind Beschreibung}" HorizontalAlignment="Center" VerticalAlignment="Center" TextTrimming="CharacterEllipsis"/>
                    <TextBlock Grid.Column="1" Foreground="White" FontSize="14" Text="{x:Bind Ausgabedatum}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    <TextBlock Grid.Column="2" Foreground="White" FontSize="14" Text="{x:Bind Rückgabedatum}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    <TextBox Grid.Column="3" Foreground="White" FontSize="14" x:Name="txtAnzahl" PlaceholderText="{x:Bind Anzahl}" TextChanged="TextBox_OnTextChanged" Width="50" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    <StackPanel Orientation="Horizontal" Grid.Column="4" Margin="-25">

                        //here I want to get the value from the TextBox named "txtAnzahl"
                        <Button Height="30" Width="30" Margin="0,10,10,10" Padding="0" Click="ButtonBase_OnClick">
                            <TextBlock FontFamily="Segoe MDL2 Assets" Foreground="LimeGreen" Text="" FontSize="20"/>
                        </Button>
                        <Button Height="30" Width="30" Margin="0,10,10,10" Padding="0">
                            <TextBlock FontFamily="Segoe MDL2 Assets" Foreground="DarkRed" Text="" FontSize="16"/>
                        </Button>

                    </StackPanel>
                </Grid>
            </DataTemplate>
        </GridView.ItemTemplate>

所以我尝试从按钮 OnClick 事件的文本框“txtanzahl”中获取值。

这是它在实时视觉树中的样子:

我尝试使用 VisualTreeHelper 来完成它,但我只找到了 GetChild 或 GetParent 的示例,但在这种情况下,它既不是孩子也不是父母。

我也无法像这样获得具有给定名称“txtAnzahl”的控件:

var anzahl = txtAnzahl.Text;

它说他不知道这个元素。

【问题讨论】:

  • 这是 UWP 还是 WPF?
  • 它是 UWP........

标签: c# xaml uwp uwp-xaml


【解决方案1】:

你可以先获取Button的父级(即StackPanel),再获取其父级的父级(即Grid),然后再下去找到TextBox

但是... 不要这样做。如果您更改了层次结构或Panel 的类型怎么办?

由于您已经知道数据模板的DataContext类型(即Ausstattung),因此您应该创建另一个属性,例如TextValue 并使其双向TextBox 绑定。然后,如果您使用ButtonCommand,您可以从CommandParameter 获取它的值,或者在代码隐藏中 -

private void ButtonBase_Click(object sender, RoutedEventArgs e)
{
    var button = (ButtonBase)sender;
    var dataContext = (Ausstattung)button.DataContext;
    var value = dataContext.TextValue;
}

你的类需要实现INotifyPropertyChanged。之后,像这样创建一个新属性 -

using System.ComponentModel;
using System.Runtime.CompilerServices;
using App1.Annotations;

namespace App1
{
    public class Ausstattung : INotifyPropertyChanged
    {
        private string _textValue;
        public string TextValue
        {
            get => _textValue;
            set
            {
                _textValue = value;
                OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

在您的 xaml 中,执行此操作 -

<TextBox Text="{x:Bind TextValue, Mode=TwoWay}" Grid.Column="3" Foreground="White" FontSize="14" x:Name="txtAnzahl" PlaceholderText="{x:Bind Anzahl}" TextChanged="TextBox_OnTextChanged" Width="50" HorizontalAlignment="Center" VerticalAlignment="Center"/>

【讨论】:

  • 双向绑定到底是什么意思?我已经拥有属性“Anzahl”,其中包含文本框中显示的值...
  • 我用 datacontext.Anzahl 尝试了你的解决方案,但它只显示了我的类“Ausstattung”中的值,但我想获取用户在 TextBox 中键入的值,然后将其写入我的类“Ausstattung”
  • 感谢您的回答,但我无法添加接口 INotifyPropertyChanged。它说它没有实现成员“INotifyPropertyChanged.PropertyChanged”,也没有找到方法“OnPropertyChanged”
  • 也许你知道,我的课程来自 wcf 网络服务
  • 非常感谢我的朋友,它现在正在工作。 PS:我也在推特上关注了你的精彩动画
【解决方案2】:

DataTemplate 中的 XAML 有点特殊;虽然它只写一次,但在运行时它将被复制并用于列表中的每个项目。这使得很难在数据模板中引用正确的元素。如果列表中有 20 个项目,txtAnzahl 可能会引用其中任何一个。

最好的解决方案是将数据模板内的内容放在单独的UserControl 中。这样您就可以像往常一样引用元素和绑定。

您可以在official docs 中阅读有关UserControl 的一些信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    • 2011-09-08
    • 1970-01-01
    • 2021-12-12
    相关资源
    最近更新 更多