0

私は ListBox を持っています。その DateTemplate は次のようになります。

<Grid Margin="30,0,0,0" HorizontalAlignment="Left">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Image Source="{Binding Path=IconSource}"/>
                <TextBlock Grid.Column="1" Background="{x:Null}" Text="{Binding Path=DisplayText, Mode=Default}" Foreground="Black"/>
            </Grid>

これは ItemContainerStyle です。

 <Style x:Key="Test" TargetType="ListBoxItem">
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border Name="Border" SnapsToDevicePixels="True">
                        <ContentPresenter/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="true">
                            <Setter TargetName="Border" Property="Background" Value="Blue" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

ListBoxItem が 1 つ選択されている場合、画像の背景を設定しないことはできますか? VS の Intellisense のように、API のアイコンには背景がありません。

4

2 に答える 2

1

最も簡単な方法は、画像を別の 内に配置しGrid、グリッドを画像に使用する背景色に設定することです。次に、親のBackgroundを​​変更すると、画像の親グリッドが背景色の変更から保護します。BackgroundBorder

于 2013-04-23T12:01:39.243 に答える
1

これがあなたの意図したものかどうかは完全にはわかりませんが、データ テンプレートを使用せず、代わりにアイテム コンテナー コントロール テンプレートの要素を使用して必要なすべてのデータ バインディングを行うことをお勧めします。これは、アイテム コンテナとデータ テンプレートが実際には同じデータ コンテキストを共有しているため可能です。の場合ListBoxItem、これは次のようになります。

<ControlTemplate x:Key="ListBoxItemTemplate" TargetType="ListBoxItem">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Image Source="{Binding IconSource}" Height="50" Margin="0,0,5,0"/>
        <Border x:Name="Border" VerticalAlignment="Center" Grid.Column="1">
            <TextBlock Text="{Binding Name}" />
        </Border>
    </Grid>
      <ControlTemplate.Triggers>
          <Trigger Property="IsSelected" Value="true">
              <Setter TargetName="Border" Property="Background" Value="Blue" />
          </Trigger>
      </ControlTemplate.Triggers>

</ControlTemplate>

ItemsContainerStyleに設定されている でこのテンプレートを参照しますListBox

要約すると、データ テンプレートは使用せず、ItemContainerStyle に組み込みます。したがって、Image 要素の背景をそのままにしておくことができます。

お役に立てれば。

于 2013-04-23T09:26:01.340 に答える