繁体   English   中英

如何从DataTemplate获取XAML Control的值

[英]How to get value of XAML Control from DataTemplate

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

        <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>

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

这是实时可视树中的样子: 在此输入图像描述

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

此外,我不能得到具有给定名称“txtAnzahl”的控件,如下所示:

var anzahl = txtAnzahl.Text;

它说他不知道这个元素。

您可以获取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"/>

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

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

您可以在官方文档中阅读一些关于UserControl的内容。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM