【问题标题】:Get to a XAML control by name in a DataTemplate在 DataTemplate 中按名称获取 XAML 控件
【发布时间】:2014-04-09 15:06:33
【问题描述】:

我有一个带有自定义 DataTemplate 的 ListView。

<ListView ItemTemplate="{StaticResource MyTemplate}"
                      ItemsSource="{Binding Items}"
                      SelectionChanged="list_SelectionChanged" />

<DataTemplate x:Key="MyTemplate">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <SECTextBox x:Name="myTextBox" Text="Test"/>
    </Grid>
</DataTemplate>

当用户更改列表视图中的项目时,如何为所选项目选择 myTextBox?

谢谢

这是使用 Windows 8.1 商店应用程序。

【问题讨论】:

  • 选择是什么意思?
  • 就像我想做 myTextBox.Text="this is what I want to do"

标签: xaml listview winrt-xaml windows-8.1


【解决方案1】:

一种方法是在您的 SecTextBox 或 Grid.... 上点击事件。

<ListView ItemTemplate="{StaticResource MyTemplate}"
                      ItemsSource="{Binding Items}"
                      SelectionChanged="list_SelectionChanged" />

<DataTemplate x:Key="MyTemplate">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <SECTextBox x:Name="myTextBox" 
                    Text="Test"
                    Tapped="HandleOnTapped"/>
    </Grid>
</DataTemplate>





 private void HandleOnTapped(object sender, TappedRoutedEventArgs e)
    {
       SECTextBox txt = (SECTextBox)sender;
       // now you have your textbox

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    • 2011-09-28
    • 1970-01-01
    • 2010-12-04
    相关资源
    最近更新 更多