如何从DataTemplate获取XAML Control的值

时间:2017-07-21 08:19:10

标签: c# xaml uwp uwp-xaml

我尝试从我的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”中获取值。

以下是实时可视树中的内容: enter image description here

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

此外,我无法使用给定名称“txtAnzahl”获取控件:

var anzahl = txtAnzahl.Text;

它说他不知道这个元素。

2 个答案:

答案 0 :(得分:3)

您可以获取Button的父级(即StackPanel),然后获取其父级的父级(即Grid),然后向下找到TextBox

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

,该怎么办?

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

Command

您的课程需要实施private void ButtonBase_Click(object sender, RoutedEventArgs e) { var button = (ButtonBase)sender; var dataContext = (Ausstattung)button.DataContext; var value = dataContext.TextValue; } 。之后,创建一个像这样的新属性 -

INotifyPropertyChanged

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

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

答案 1 :(得分:0)

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

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

您可以在official docs中了解{{1}}。