WPF DataTemplateKey 找不到 ListBox 键

发布于 2024-08-29 20:34:12 字数 4643 浏览 3 评论 0原文

我对 WPF 相当陌生,并且无法让 DataTemplateKey 找到我的列表框。

<Window.Resources>

    <ControlTemplate x:Key="FocusTemplate" >
        <Rectangle Fill="Azure" Width="290" Height="55" />
    </ControlTemplate>
    <Style x:Key="FocusStyle" TargetType="{x:Type Control}">
        <Setter Property="Template" Value="{StaticResource FocusTemplate}"/>
    </Style>

    <Style TargetType="ListBoxItem">
        <EventSetter Event="GotFocus" Handler="ListItem_GotFocus"></EventSetter>
    </Style>

    <DataTemplate DataType="{x:Type TextBlock}">
    </DataTemplate>

    <DataTemplate x:Key="CustomListData" DataType="{x:Type ListBox}">
        <Border BorderBrush="Black" BorderThickness="1" Margin="-2,0,0,-1">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="55*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform ScaleX="1" ScaleY="1"/>
                        <SkewTransform AngleX="0" AngleY="0"/>
                        <RotateTransform Angle="0"/>
                        <TranslateTransform X="0" Y="0"/>
                    </TransformGroup>
                </Grid.RenderTransform>
                <!--<ScrollViewer x:Name="PART_ContentHost" />-->
                <TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent"
                                         Foreground="#FF6FB8FD"
                                         FontSize="18"
                                         FocusVisualStyle="{StaticResource FocusStyle}"
                                         Name="editingBox"
                                         TextWrapping="Wrap"
                                         Text="{Binding .}"
                                         Grid.Column="1"
                                         Grid.Row="1"
                                         MinHeight="55"
                                         Cursor="Hand"
                                         IsReadOnly="True"
                                         >
                    <TextBox.Background>
                        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                            <LinearGradientBrush.RelativeTransform>
                                <TransformGroup>
                                    <ScaleTransform CenterX="0.5" CenterY="0.5"/>
                                    <SkewTransform CenterX="0.5" CenterY="0.5"/>
                                    <RotateTransform Angle="0" CenterX="0.5" CenterY="0.5"/>
                                    <TranslateTransform/>
                                </TransformGroup>
                            </LinearGradientBrush.RelativeTransform>
                            <GradientStop Color="#FF2D4984"/>
                            <GradientStop Color="#FF182D56" Offset="0.042"/>
                        </LinearGradientBrush>
                    </TextBox.Background>

                </TextBox>
            </Grid>
        </Border>
    </DataTemplate>

    <Style TargetType="{x:Type ListBox}">
        <Setter Property="ItemTemplate" Value="{StaticResource CustomListData }" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
    </Style>

  </Window.Resources>

  <Window.DataContext>
    <ObjectDataProvider 
  ObjectType="{x:Type local:ImageLoader}" 
  MethodName="LoadImages" 
  />
  </Window.DataContext>

    <ListBox ItemsSource="{Binding}" Width="320" Background="#FF021422" BorderBrush="#FF1C4B79">

        <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">Transparent</SolidColorBrush>
        </ListBox.Resources>

    </ListBox>

下面的代码会找到一个TextBlock,

var key = new System.Windows.DataTemplateKey(typeof(TextBlock));
var r = (DataTemplate)this.FindResource(key);

但是,当我将类型更改为ListBox时,找不到该键。我错过了什么?

谢谢 瑞安

I am fairly new to WPF and am having trouble getting the DataTemplateKey to find my ListBox.

<Window.Resources>

    <ControlTemplate x:Key="FocusTemplate" >
        <Rectangle Fill="Azure" Width="290" Height="55" />
    </ControlTemplate>
    <Style x:Key="FocusStyle" TargetType="{x:Type Control}">
        <Setter Property="Template" Value="{StaticResource FocusTemplate}"/>
    </Style>

    <Style TargetType="ListBoxItem">
        <EventSetter Event="GotFocus" Handler="ListItem_GotFocus"></EventSetter>
    </Style>

    <DataTemplate DataType="{x:Type TextBlock}">
    </DataTemplate>

    <DataTemplate x:Key="CustomListData" DataType="{x:Type ListBox}">
        <Border BorderBrush="Black" BorderThickness="1" Margin="-2,0,0,-1">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="55*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <Grid.RenderTransform>
                    <TransformGroup>
                        <ScaleTransform ScaleX="1" ScaleY="1"/>
                        <SkewTransform AngleX="0" AngleY="0"/>
                        <RotateTransform Angle="0"/>
                        <TranslateTransform X="0" Y="0"/>
                    </TransformGroup>
                </Grid.RenderTransform>
                <!--<ScrollViewer x:Name="PART_ContentHost" />-->
                <TextBox Width="290" TextAlignment="Left" VerticalContentAlignment="Center" BorderThickness="0" BorderBrush="Transparent"
                                         Foreground="#FF6FB8FD"
                                         FontSize="18"
                                         FocusVisualStyle="{StaticResource FocusStyle}"
                                         Name="editingBox"
                                         TextWrapping="Wrap"
                                         Text="{Binding .}"
                                         Grid.Column="1"
                                         Grid.Row="1"
                                         MinHeight="55"
                                         Cursor="Hand"
                                         IsReadOnly="True"
                                         >
                    <TextBox.Background>
                        <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                            <LinearGradientBrush.RelativeTransform>
                                <TransformGroup>
                                    <ScaleTransform CenterX="0.5" CenterY="0.5"/>
                                    <SkewTransform CenterX="0.5" CenterY="0.5"/>
                                    <RotateTransform Angle="0" CenterX="0.5" CenterY="0.5"/>
                                    <TranslateTransform/>
                                </TransformGroup>
                            </LinearGradientBrush.RelativeTransform>
                            <GradientStop Color="#FF2D4984"/>
                            <GradientStop Color="#FF182D56" Offset="0.042"/>
                        </LinearGradientBrush>
                    </TextBox.Background>

                </TextBox>
            </Grid>
        </Border>
    </DataTemplate>

    <Style TargetType="{x:Type ListBox}">
        <Setter Property="ItemTemplate" Value="{StaticResource CustomListData }" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
    </Style>

  </Window.Resources>

  <Window.DataContext>
    <ObjectDataProvider 
  ObjectType="{x:Type local:ImageLoader}" 
  MethodName="LoadImages" 
  />
  </Window.DataContext>

    <ListBox ItemsSource="{Binding}" Width="320" Background="#FF021422" BorderBrush="#FF1C4B79">

        <ListBox.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}">Transparent</SolidColorBrush>
        </ListBox.Resources>

    </ListBox>

The following code will find a TextBlock

var key = new System.Windows.DataTemplateKey(typeof(TextBlock));
var r = (DataTemplate)this.FindResource(key);

However, when I change the type to ListBox, the key can not be found. What have I missed?

Thanks
Ryan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后来的我们 2024-09-05 20:34:12

您已为 ListBox DataTemplate 指定了一个不同的密钥,具体来说,x:Key="CustomListData"
x:Key 属性将优先于根据 DataType 生成的自动 DataTemplateKey。删除该键,就会找到自动的 DataTemplateKey

You have given your ListBox DataTemplate a different key, specifically, x:Key="CustomListData".
The x:Key attribute will take precedence over the automatic DataTemplateKey that is generated based on the DataType. Delete that key and the automatic DataTemplateKey will be found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文