在我的應用程序中,我爲ComboBox創建了一個DataTemplate。 (請參閱下面的XAML。)我想將此樣式應用於應用程序中的多個ComboBox。我怎樣才能做到這一點?WPF:將相同的ItemTemplate應用於多個組合框
<ComboBox Name="mouseColorCmbx" AutomationProperties.AutomationId="SimulationOptionsPanel_mouseColorCmbx" Grid.Column="1" Grid.Row="0" Margin="0 0 0 0" HorizontalAlignment="Stretch" VerticalAlignment="Center">
<ComboBox.ItemTemplate>
<DataTemplate DataType="Forms:ColorInfo">
<StackPanel Orientation="Horizontal" Margin="0,3,0,0">
<Border Margin="2,0,2,0" BorderThickness="1">
<Border.BorderBrush>
<SolidColorBrush Color="Black"/>
</Border.BorderBrush>
<Border.Background>
<SolidColorBrush Color="{Binding Path=Color}"/>
</Border.Background>
<Rectangle Height="10" Width="25"/>
</Border>
<TextBlock Text="{Binding Path=Name}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我試着將DataTemplate添加到此用戶控件的Resources中。 (我將它命名爲template1。)然後我嘗試將組合框的ItemTemplate屬性設置爲template1。 – akmartzke
我其實犯了一個愚蠢的錯誤。我試圖在我的樣式上引用Name屬性而不是Key屬性。我的風格不適用於我的控制,因爲它無法找到。 – akmartzke