如何在XAML中设置ComboBoxItems的样式表
这里我提到了每个ComboBoxItem的样式表。我只想引用一次样式表(关于ComboBoxItem)。这样,当动态数据项添加到样式表中时,样式表将自动应用。您将需要使用如下内容:如何在XAML中设置ComboBoxItems的样式表,xaml,windows-phone-8,windows-phone,windows-phone-8.1,windows-phone-8-emulator,Xaml,Windows Phone 8,Windows Phone,Windows Phone 8.1,Windows Phone 8 Emulator,这里我提到了每个ComboBoxItem的样式表。我只想引用一次样式表(关于ComboBoxItem)。这样,当动态数据项添加到样式表中时,样式表将自动应用。您将需要使用如下内容: <ComboBox Foreground="Black" BorderBrush="Black" HorizontalAlignment="Left" Margin="108,280,0,0" VerticalAlignment="Top" Width="200
<ComboBox
Foreground="Black"
BorderBrush="Black"
HorizontalAlignment="Left" Margin="108,280,0,0" VerticalAlignment="Top" Width="200" Style="{StaticResource ComboBoxStyl}"><ComboBoxItem Content="One" IsSelected="True" Style="{StaticResource ComboBoxItemStyle1}"/>
<ComboBoxItem Content="Two" Style="{StaticResource ComboBoxItemStyle1}"/>
<ComboBoxItem Content="Three" Style="{StaticResource ComboBoxItemStyle1}"/>
<ComboBoxItem Content="Four" Style="{StaticResource ComboBoxItemStyle1}"/>
<ComboBoxItem Content="Five" Style="{StaticResource ComboBoxItemStyle1}"/>
</ComboBox>
然后在页面中创建一个列表ItemList
,并在页面的构造函数中,使用您想要的任何数据初始化列表。然后在XAML数据模板中,只需将“{Binding ItemText}”
插入希望文本出现的任何位置(就像我在文本块中所做的那样)。
使用“ItemContainerStyle”引用我们为ComboBoxItems创建的sylesheet,我们可以轻松更改ComboxItems的样式,无论是静态声明还是动态声明。使用
ObservableCollection
进行绑定比使用列表
更好<代码>列表s在更改时不更新绑定,因为它们不实现INotifyCollectionChanged
和INotifyPropertyChanged
,但observecollection
s执行。要将单个样式应用于所有项目,您可以像这样使用ItemContainerStyle
<ComboBox x:Name="combobox1" ItemsSource="{Binding ItemList}">
<ComboBox.ItemTemplate>
<DataTemplate>
<!-- define data template and/or style, example: -->
<TextBlock Style="{StaticResource TextBlockItemStyle}" Text="{Binding ItemText}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
public class ComboBoxListItem
{
public string ItemText {get; set;}
//any other useful property
}
<ComboBox
SelectedIndex="0"
Foreground="Black"
ItemContainerStyle="{StaticResource ComboBoxItemStyle1}"
BorderBrush="Black"
HorizontalAlignment="Left" Margin="108,280,0,0" VerticalAlignment="Top" Width="200" Style="{StaticResource ComboBoxStyl}">
<ComboBoxItem Content="One" />
<ComboBoxItem Content="Two" />
<ComboBoxItem Content="Three" />
<ComboBoxItem Content="Four" />
<ComboBoxItem Content="Five" />
</ComboBox>