- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
目前,我已经对应该显示的 DataContext 对象的属性 (DisplayName) 进行了硬编码。但通常您在 ComboBox 本身中使用属性 DisplayMemberPath 指定此路径。如何使用 DisplayMemberPath 指定的值作为要在内容呈现器中绑定(bind)的属性?
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="ItemBorder"
Padding="2,0"
BorderThickness="1"
CornerRadius="3">
<ContentPresenter Content="{Binding DisplayName}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger SourceName="ItemBorder" Property="IsMouseOver" Value="True">
<Setter TargetName="ItemBorder" Property="Background" Value="{StaticResource LightBlueBackgroundBrush}"/>
<Setter TargetName="ItemBorder" Property="BorderBrush" Value="{StaticResource LightBlueBackgroundBrush}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
作为引用,这是我的 ComboBox 样式。
<Style TargetType="{x:Type ComboBox}">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
<Setter Property="Height" Value="22"/>
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="{StaticResource MidGreyBorderStroke}"/>
<Setter Property="Border.CornerRadius" Value="3" />
<Setter Property="IsEditable" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="1"
CornerRadius="{TemplateBinding Border.CornerRadius}"
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter x:Name="ReadOnlyContentPresenter"
Grid.Column="0"
Margin="5,0"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Content="{TemplateBinding SelectionBoxItem}"
ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}" />
<TextBox x:Name="PART_EditableTextBox"
Grid.Column="0"
Margin="5,0"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Visibility="Hidden"
IsReadOnly="{TemplateBinding IsReadOnly}" />
<Border Grid.Column="1"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="1,0,0,0"
Margin="0,2" />
<ToggleButton Grid.Column="2"
Margin="1,0"
Background="{TemplateBinding Background}"
VerticalAlignment="Center"
HorizontalAlignment="Right"
Focusable="False"
IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
ClickMode="Press"
Style="{StaticResource ComboBoxToggleButton}"/>
<Popup x:Name="PART_Popup"
AllowsTransparency="True"
Placement="Bottom"
IsOpen="{TemplateBinding IsDropDownOpen}"
Focusable="False"
PopupAnimation="Fade"
SnapsToDevicePixels="True">
<Grid Background="Transparent"
MinWidth="{TemplateBinding ActualWidth}"
MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="1"
CornerRadius="{TemplateBinding Border.CornerRadius}"
Margin="0,2,0,0">
<ScrollViewer Margin="6,0">
<StackPanel IsItemsHost="True"
KeyboardNavigation.DirectionalNavigation="Contained" />
</ScrollViewer>
</Border>
</Grid>
</Popup>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value=".65" />
</Trigger>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="DropDownBorder" Property="MinHeight" Value="42"/>
</Trigger>
<Trigger Property="IsEditable" Value="True">
<Setter Property="IsTabStop" Value="false"/>
<Setter TargetName="PART_EditableTextBox" Property="Visibility" Value="Visible"/>
<Setter TargetName="ReadOnlyContentPresenter" Property="Visibility" Value="Hidden"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
最佳答案
嗯,这实际上是一个有趣的问题,答案很简单。
简答:
<ContentPresenter />
长答案:
如果您已经覆盖了 ComboBoxItem
样式,但您仍想使用 ComboBox
中的 DisplayMemberPath
,则无需将任何内容放入样式 Content
绑定(bind)为 Parent
(ComboBox) 将为您解决此问题。因为 DisplayMemberPath
只是一个字符串而不是实际属性,如果您绑定(bind)到 DisplayMemberPath
,您的所有项目将只显示您放入 DisplayMemberPath
中的任何值/p>
因此,您所要做的就是从 ComboBoxItem
样式中删除 Content
绑定(bind)
例子:
<Style TargetType="{x:Type ComboBoxItem}">
.............
<Border x:Name="ItemBorder" Padding="2,0" BorderThickness="1" CornerRadius="3">
<ContentPresenter /> <!-- no content binding -->
</Border>
这是一个简单的例子,说明了这个工作
Xaml:
<Window x:Class="WpfApplication13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" x:Name="UI" Width="343" Height="744.625" >
<Window.Resources>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBoxItem">
<Border x:Name="ItemBorder" Padding="2,0" BorderThickness="1" CornerRadius="3">
<ContentPresenter />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<StackPanel DataContext="{Binding ElementName=UI}">
<ComboBox ItemsSource="{Binding Items}" DisplayMemberPath="Name" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"/>
<ComboBox ItemsSource="{Binding Items}" DisplayMemberPath="State" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120"/>
</StackPanel>
</Window>
代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 50; i++)
{
Items.Add(new ComboBoxModel { Name = "Name" + i, State = "State" + i });
}
}
private ObservableCollection<ComboBoxModel> _items = new ObservableCollection<ComboBoxModel>();
public ObservableCollection<ComboBoxModel> Items
{
get { return _items; }
set { _items = value; }
}
}
public class ComboBoxModel
{
public string Name { get; set; }
public string State { get; set; }
}
结果:
关于c# - 在 ComboBoxItem 模板中使用 DisplayMemberPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15283639/
我的项目中有以下ComboBox,但是如果我这样写的话,很困扰我:
我的目标是拥有一个 ComboBox,其中每个下拉项目都有一个特定的文本和一个与之关联的特定项目,例如,如果有人点击“blah” - 所选项目将是 3. 据我所知 - 只有一个“内容”同时代表文本和值
我希望我的 ComboBox 像这样在多个列中显示 ComboBoxItem: 谁能告诉我该怎么做? 我可以用这个:
加载控件时,我的组合框项目选择中的内容未显示在组合框中。它在我鼠标悬停后出现,然后开始工作。 这是默认选中的组合框项目
我正在处理通常包含大量数据的 ComboBox 元素; ~250000 个数据条目。 当 ComboBox 像这样设置时,这会很好地工作。
我有一个小应用程序,其中相同的 ComboBox 出现在多个位置,并且它始终具有相同的项目集。而不是每次我使用组合框时都这样做: 我在想最好让项目成为我可以
我想为 PASS 设置文本颜色如 GREEN和 FAIL 的文本颜色如 RED .我似乎无法找到解决方案。我需要在纯 XAML 中执行此操作。 最
我正在尝试以编程方式填充 ComboBox。我正在创建 ComboBoxItems 并想设置它们的文本(对最终用户可见的文本)和它们的值(用户选择它后我将在后台处理的对象。 然而,ComboBoxIt
我目前有一个如下所示的组合框: //XAML Awake & Alive 这会引发错误:以与符号“&”开头的实体引用或序列必须以分号“;”终止。 我假设我缺少某种转义序列以允许我使用 &。如何设
如何从 SmartGWT ComboBoxItem 获取所有记录? 我将记录添加为数据源。没有找到任何方法来检索所有记录。 提前谢谢您! 最佳答案 尝试 RecordList recordList=c
在下拉菜单中我得到这样的结果。找到附件图片。实际上,在“名称”字段中,“名称”和“描述”都显示为逗号(,)分隔。 final ComboBoxItem comboBoxItem = new Combo
我有一个看起来像这样的样式: var comboBoxItemStyle = new Style(typeof(ComboBoxItem)); comboBoxItemStyle.Setters.Ad
您好,我刚刚构建了一个小型转换计算器,我在其中添加了一个组合框,因此它不会那么困惑且易于管理。我不会向我的组合框添加一些选项,以便用户有不同的选项可供选择。但是,我将构建一个带有转换计算的小类,以便在
我有一个绑定(bind)到 ObservableCollection 的 ComboBox:
我有以下示例代码。奇怪的是,MouseMove 事件正确触发,但是当替换为 MouseEnter 时,当鼠标移到 ComboBoxItem 上时没有任何反应。知道如何解决这个问题吗?我实际上需要在用户
我有一个使用 DataTemplate 的 ListBox。单个 ListBoxItems 显示为 TextBlock 和 ComboBox。我现在想为 ListBoxItems 使用 Style,而
我正在使用标准的 wpf/mvvm 应用程序,我将组合框绑定(bind)到 ViewModel 上的集合。 我需要能够从下拉列表中取消选择一个项目。意思是,用户应该能够选择一些东西,然后决定他们想要取
如何获取 ComboBoxItem 的 ParentComboBox? 如果按下 Insert-Key,我想关闭一个打开的 ComboBox: var focusedElement = Keyboa
我需要找到 ComboBoxItem 所在的 ComboBox。 在代码隐藏中,我在单击 ComboBoxItem 时捕获了一个事件,但我不知道特定 ComboBoxItem 属于几个 ComboBo
我正在尝试在 smartgwt 的 ComboBoxItem 中添加带有 null 值的空白行,该值已绑定(bind)到来自数据库的数据源。 我也尝试过服务层、 Controller 层和数据库的改变
我是一名优秀的程序员,十分优秀!