我想要做的是一个组合框,顶部有最喜欢的值,不同的背景颜色和按钮。现在我有:创建一个WPF自定义组合框
<UserControl x:Class="ComboBoxWithButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
Name="root"
d:DesignWidth="300" Height="25">
<ComboBox
x:Name="ComboBoxBtn"
VerticalAlignment="Top"
HorizontalAlignment="Left"
Margin="0,0,0,-1"
Width="300"
ItemsSource="{Binding Source, RelativeSource={RelativeSource AncestorType=UserControl}}"
SelectedItem="{Binding Path=Selected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=UserControl}}">
<ComboBox.Resources>
<Style TargetType="ComboBoxItem">
????
</Style>
</ComboBox.Resources>
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.Style>
<Style TargetType="Grid">
<Setter Property="Background" Value="#FFE6E6FA"/>
</Style>
</Grid.Style>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Label Content="{Binding}" Width="250" />
<Button Grid.Column="1" Command="{Binding CommandButton, ElementName=root}"
CommandParameter="{Binding}">+</Button>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</UserControl>
现在我有一个添加按钮,所以我可以添加我的项目作为收藏夹。但我现在想要的是,根据我所代表的项目,我喜欢或不喜欢。
案例是一个最喜欢的具有不同的背景色和[ - ]按钮(删除)。案例不是背景是像往常一样白,并有一个[+]。
如果你想添加按钮,你将需要修改ComboBox控件模板,在这里,https://msdn.microsoft.com/en-us/library/dd334408%28v=vs.95% 29.aspx – 0x4f3759df
我的主要问题是不是按钮。我是如何“说”如果该项目是一个最喜欢的(在一个方法的帮助下,检查他是否最喜欢)然后做到这一点,否则做到这一点。 –
您可以使用两个控件的网格。一个控件具有绑定可见性= IsFavorite,另一个可见性= IsNotFavorite – 0x4f3759df