ListBox со стилем и Datatemplate - PullRequest
0 голосов
/ 30 апреля 2018

У меня есть стиль для ListBox и ListBoxItem

<Style TargetType="{x:Type ListBox}">
        <Setter Property="Background" Value="{StaticResource DarkBackground}"/>
    </Style>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" Value="{StaticResource LightBackground}"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Margin" Value="5,0,5,0"/>
        <Setter Property="Padding" Value="5,0,5,0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border Background="{TemplateBinding Background}" Height="35"
                                            BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <ItemsControl Focusable="False" Margin="{TemplateBinding Padding}" Foreground="{TemplateBinding Foreground}" VerticalContentAlignment="Center">
                            <ContentPresenter Tag="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" Content="{Binding Path=Content, RelativeSource={RelativeSource TemplatedParent}}"/>
                        </ItemsControl>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="{StaticResource GlassFX}"/>
                            <Setter Property="BorderBrush" Value="White"/>
                            <Setter Property="BorderThickness" Value="1"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Я добавляю новый ListBox в MainWindow:

<ListBox ItemsSource="{Binding AvailableClient}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <ListBoxItem Tag="{Binding Path=ID}" Content="{Binding Path=Name}"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

Но моя привязка не работает ...

Если изменить стиль

<ContentPresenter Tag="{Binding Path=Tag.ID, RelativeSource={RelativeSource TemplatedParent}}" Content="{Binding Path=Content.Name, RelativeSource={RelativeSource TemplatedParent}}"/>

Все работают. Но мне нужен универсальный стиль для многих списков. Как я могу это сделать?

1 Ответ

0 голосов
/ 01 мая 2018

Если вы используете VisualStudio, создайте новый Resource Dictionary в своем решении, а затем скопируйте в него свой стиль следующим образом:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type ListBox}">
        -Your style code here-
    </Style>
</ResourceDictionary>

Затем, либо в своих ресурсах Window / Usercontrol, либо, предпочтительно, в файле App.xaml, загрузите словарь ресурсов следующим образом:

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="whatever you named the file.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Теперь ваш собственный стиль должен переопределять стиль для всех списков в вашем решении. Если вы хотите использовать свой стиль в другом решении, просто поместите созданный вами файл xaml в свое решение и загрузите его, как указано выше.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...