字典中的ComboBox(WPF)模板

时间:2011-11-16 15:10:31

标签: c# wpf templates combobox

我正在用wpf练习C#,我正在尝试使用模板格式化 这时,我有一个自定义的组合框绑定链接:

<ComboBox Height="23" HorizontalAlignment="Left" Margin="177,28,0,0" Name="JoinedFiles_combobox" VerticalAlignment="Top" Width="164" Grid.Column="1" SelectionChanged="JoinedFiles_combobox_SelectionChanged">
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <Canvas Height="18">
        <Image Name="ImageName" Height="16" Width="16" Canvas.Left="1" Canvas.Top="1"/>
        <TextBlock Text="{Binding Name}" Canvas.Left="26" Canvas.Top="1" Width="120"/>
      </Canvas>
      <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding PictureID}" Value="0">
          <Setter TargetName="ImageName" Property="Source" Value="Resources\0.png"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding PictureID}" Value="1">
          <Setter TargetName="ImageName" Property="Source" Value="Resources\1.png"/>
        </DataTrigger>
      </DataTemplate.Triggers>
    </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>   

由于我想在另一个xaml文件(另一个窗口)中使用另一个这样的组合框,我试图将其作为模板放在我的app.xaml文件中。这将防止复制代码并简化操作。

这是我在app.xaml文件中写的:

<Application.Resources>
  <ResourceDictionary>
    <ControlTemplate x:Key="CustomisedComboBox" TargetType="{x:Type ComboBox}">
      <ItemTemplate>
        <DataTemplate>
          <Canvas Height="18">
            <Image Name="ImageName" Height="16" Width="16" Canvas.Left="1" Canvas.Top="1"/>
            <TextBlock Text="{Binding Name}" Canvas.Left="26" Canvas.Top="1" Width="120"/>
          </Canvas>
          <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding PictureID}" Value="0">
              <Setter TargetName="ImageName" Property="Source" Value="Resources\0.png"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding PictureID}" Value="1">
              <Setter TargetName="ImageName" Property="Source" Value="Resources\1.png"/>
            </DataTrigger>
          </DataTemplate.Triggers>
        </DataTemplate>
      </ItemTemplate>
    </ControlTemplate>
  </ResourceDictionary>
</Application.Resources>  

问题是它在我的控件模板中找不到ItemTemplate。我怎样才能给它那个项目模板?

1 个答案:

答案 0 :(得分:2)

我认为您需要Style,而不是ControlTemplate

<Style x:Key="CustomisedComboBox" TargetType="{x:Type ComboBox}">
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <Canvas Height="18">
                    <Image Name="ImageName" Height="16" Width="16" Canvas.Left="1" Canvas.Top="1"/>
                    <TextBlock Text="{Binding Name}" Canvas.Left="26" Canvas.Top="1" Width="120"/>
                </Canvas>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding PictureID}" Value="0">
                        <Setter TargetName="ImageName" Property="Source" Value="Resources\0.png"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding PictureID}" Value="1">
                        <Setter TargetName="ImageName" Property="Source" Value="Resources\1.png"/>
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

ControlTemplate定义了在Style定义控件属性值时如何呈现控件。