ControlTemplate中的动态DataTemplate通过触发器

时间:2016-05-16 00:12:22

标签: c# wpf xaml

我有一个列表框" listBox_Results"和几个ItemTemplates(其中一个是ItemTemplateStyle1),在我的ItemContainerStyle中设置了item的Template属性。所以我想在触发器中更改我的ItemTemplate" IsSelected"。 (一般来说:我希望我的listboxitem通过动态设置不同的ItemTemplate在选择时更改大小和内容显示) 你有什么解决办法?

祝你好运

upd:如果你认为这个问题不清楚或没有用,那么大多数人如果在你减去之前告诉你为什么会这样做

代码:

   <ListBox Name="listBox_Results"  
             HorizontalAlignment="Stretch"
             VerticalAlignment="Stretch"
             BorderThickness="0"
             Margin="2"
             Grid.Row="0"
             ItemTemplate="{StaticResource ItemTemplateStyle1}"
             ItemsSource="{Binding}" >
            <ListBox.ItemContainerStyle>

                <Style TargetType="{x:Type ListBoxItem}">
                    <Setter Property="Background" Value="Transparent"/>
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                    <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
                    <Setter Property="Padding" Value="2,2,2,2"/>
                    <Setter Property="Margin" Value="2"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                                <Border x:Name="Bd" Margin="1" SnapsToDevicePixels="true" CornerRadius="3" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" >
                                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                </Border>
                                <ControlTemplate.Triggers>
                                    <Trigger Property="IsSelected" Value="true">
                                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                                        <Setter Property="FontWeight" Value="Bold"/>
                                        <Setter Property="Background" TargetName="Bd">
                                            <Setter.Value>
                                                #E1E1E1
                                            </Setter.Value>
                                        </Setter>
                                        ...

2 个答案:

答案 0 :(得分:1)

首先,取出内联样式并创建一个ResourceDictionary来保持一致。这也有助于我建议的模板切换。

在资源字典中,您将定义所需的两个模板(选定和未选择的列表项模板),列表项的样式和列表框本身。我正在缩写代码,只是为了说明我如何将这些项目放在一起。

在ResourceDictionary

<ControlTemplate x:Key="unselectedTemplate" TargetType="{x:Type ListBoxItem}">
   <Grid>
     <ContentPresenter />
   </Grid>
</ControlTemplate>
<ControlTemplate x:Key="selectedTemplate" TargetType="{x:Type ListBoxItem}">
   <Grid>
     <ContentPresenter Margin="3"/>
   </Grid>
</ControlTemplate>
<Style x:Key="listboxItemStyle" TargetType="{x:Type ListBoxItem}">
   <Setter Property="Template" Value="{StaticResource unselectedTemplate}"/>
   <Style.Triggers>
     <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Self}}" Value="True">
       <Setter Property="Template" Value="{StaticResource selectedTemplate}"/>
     </DataTrigger>
    </Style.Triggers>
</Style>
<Style x:Key="listBoxStyle" TargetType="{x:Type ListBox}">
   <Setter Property="HorizontalAlignment" Value="Stretch"/>
   <Setter Property="ItemContainerStyle" Value="{StaticResource listboxItemStyle}"/>
</Style>

然后,当您在页面上创建列表框时......只需引用列表框样式键。

<ListBox Name="listbox_Results" Style="{StaticResource listBoxStyle}" ItemsSource="{Binding}"/>

确保在样式之前定义ControlTemplates,我发现当我没有遇到错误时。此外,这可以使您的布局页面更清晰,如果您需要再次使用它们,则样式更容易重复使用。

I uploaded a very basic example here.

答案 1 :(得分:0)

您必须使用数据模板选择器,它将根据选择器中的条件选择特定的数据模板。

您必须使用单独的名称在xaml中编写数据模板,并从DataTemplateSelector类文件中选择它们。 为此,您需要从基类 DataTemplateSelector

继承

我将与您分享一些示例代码。请检查一下,您将了解如何使用项目模板选择器。

XAML:

Bundle b = new Bundle();
b.putStringArray("files",
mChecked.toArray(new String[mChecked.size()]));
Intent intent = new Intent(ActivityB.this,ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtras(b);
setResult(RESULT_OK, intent);
finish();

代码背后:

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication5"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate x:Key="NormalUserDataTemplate">
            <StackPanel>
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
        <DataTemplate x:Key="PremiumUserDataTemplate">
            <StackPanel Background="LightBlue">
                <TextBlock Text="{Binding Name}" />
            </StackPanel>
        </DataTemplate>
        <local:PremiumUserDataTemplateSelector x:Key="myPremiumUserDataTemplateSelector" />
    </Window.Resources>
    <Grid>
        <ListView x:Name="myListView" ItemTemplateSelector="{StaticResource myPremiumUserDataTemplateSelector}">
        </ListView>
    </Grid>
</Window>