簡體   English   中英

如何在DataTemplate中選擇單個TextBlock

[英]How to select a single TextBlock in a DataTemplate

我的WPF應用程序中有此DataTemplate

當我單擊一個按鈕時,我需要獲取TextBlock Name="titleCategory"並更改其顏色。

目前,我無法選擇TextBlock

您能給我一個簡單的解決方案嗎?

<DataTemplate x:Key="CategoriesSelectedDataTemplate">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Border d:LayoutOverrides="Width, Height"
                CornerRadius="5"
                HorizontalAlignment="Stretch"
                Background="Transparent" >
            <TextBlock Name="titleCategory" TextWrapping="Wrap"
                       Text="{Binding CapitalizedDescription, FallbackValue=Category}"
                       Foreground="Red"
                       />
        </Border>
        <!-- 1L main menu HOVER -->
        <Custom:SurfaceListBox
            d:LayoutOverrides="Width"
            ItemsSource="{Binding Shops}"
            ItemTemplate="{DynamicResource ShopUnselectedSurfaceListDataTemplate}"
            Grid.RowSpan="1"
            Grid.Row="1"
            SelectionChanged="shopListBox_SelectionChanged"
            ItemContainerStyle="{DynamicResource CategoriesSurfaceListBoxItemStyle}"
            SelectionMode="Multiple"/>
    </Grid>
</DataTemplate>

我用這段代碼解決了

            var elem = (FrameworkElement)sender;
            TextBlock myTextBlock = (TextBlock)elem.FindName("titleCategory");
            myTextBlock.Foreground = System.Windows.Media.Brushes.Yellow;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM