好吧,这一直困扰着我一段时间.我想知道其他人如何处理以下情况:
<ComboBox ItemsSource="{Binding MyItems}" SelectedItem="{Binding SelectedItem}"/>
Run Code Online (Sandbox Code Playgroud)
DataContext对象的代码:
public ObservableCollection<MyItem> MyItems { get; set; }
public MyItem SelectedItem { get; set; }
public void RefreshMyItems()
{
MyItems.Clear();
foreach(var myItem in LoadItems()) MyItems.Add(myItem);
}
public class MyItem
{
public int Id { get; set; }
public override bool Equals(object obj)
{
return this.Id == ((MyItem)obj).Id;
}
}
Run Code Online (Sandbox Code Playgroud)
显然,当RefreshMyItems()调用该方法时,组合框会收到Collection Changed事件,更新其项目并且在刷新的集合中找不到SelectedItem =>将SelectedItem设置为null.但我需要组合框使用Equals方法来选择新集合中的正确项目.
换句话说 - ItemsSource集合仍然包含正确的MyItem,但它是一个new对象.而且我希望组合框能够使用类似的东西Equals来自动选择它(这更加困难,因为首先是源集合调用Clear()重置集合并且此时已将SelectedItem设置为null).
更新2在复制粘贴下面的代码之前,请注意它远非完美!请注意,默认情况下它不会绑定两种方式.
更新 …
例如,请注意文本不完全位于ComboBox的垂直中心.

这是我的XAML:
<Window x:Class="_24HoursBook.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="350" MinHeight="450" MinWidth="350">
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="0.15*" />
<RowDefinition />
</Grid.RowDefinitions>
<Image Grid.Row="0" Stretch="Fill" Source="Image/topBarBg.png" />
<StackPanel Orientation="Horizontal" Grid.Row="0">
<TextBlock Text="Platform"
Foreground="White"
FontFamily="Georgia"
FontSize="15"
Margin="10"
HorizontalAlignment="Center"
VerticalAlignment="Center"/>
<ComboBox x:Name="cmbPlatform"
Margin="10"
FontFamily="Georgia"
FontSize="15"
MinHeight="30"
MinWidth="140"
VerticalAlignment="Center">
<ComboBoxItem>All Platforms</ComboBoxItem>
<ComboBoxItem>Playstation 3</ComboBoxItem>
<ComboBoxItem>XBox 360</ComboBoxItem>
<ComboBoxItem>Wii</ComboBoxItem>
<ComboBoxItem>PSP</ComboBoxItem>
<ComboBoxItem>DS</ComboBoxItem>
</ComboBox>
</StackPanel>
<Image Grid.Row="0" Source="Image/about.png"
Height="16" HorizontalAlignment="Right"
VerticalAlignment="Center"
Margin="0 0 10 0" />
<ListView Grid.Row="1" Background="#343434">
</ListView>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud) 我的问题与此问题类似:如何在C#组合框或文本框中动态更改自动完成条目? 但我仍然没有找到解决方案.
问题简述:
我有一个ComboBox和大量的记录显示在其中.当用户开始输入时,我想加载以输入文本开头的记录,并为用户提供自动完成功能.如上面的主题所述,我无法加载它们?omboBox_TextChanged因为我总是覆盖以前的结果而从未看到它们.
我可以只使用它ComboBox吗?(不TextBox或ListBox)
我用这个设置:
?omboBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
?omboBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
Run Code Online (Sandbox Code Playgroud) 我正在使用Designer和WinForms在C#,Visual Studio 2008中编写GUI.我有一个ComboBox控件,我希望它只允许从提供的选项中进行选择而不接受用户输入的字符串.它似乎没有ReadOnly属性,禁用它会阻碍控件的可读性(以及禁止用户选择).
我在表单顶部有一个组合框,可将可编辑数据加载到下面的字段中.如果用户进行了更改但未保存,并尝试从组合框中选择其他选项,我想警告他们并给他们取消或保存的机会.
我需要一个带有可取消事件参数的"BeforeValueChange"事件.
有关如何完成的任何建议?
假设我有一个包含四个值的枚举:
public enum CompassHeading
{
North,
South,
East,
West
}
Run Code Online (Sandbox Code Playgroud)
使用这些项目填充ComboBox需要什么样的XAML?
<ComboBox ItemsSource="{Binding WhatGoesHere???}" />
Run Code Online (Sandbox Code Playgroud)
理想情况下,我不必为此设置C#代码.
我试图创建一个简单的ComboBox:
var combo1 = new Ext.form.ComboBox({
store: [1,2,3],
renderTo: document.body
});
Run Code Online (Sandbox Code Playgroud)
但是以这种方式编写它的行为很奇怪:
我将我的代码与Ext主页上的示例进行了比较,发现添加triggerAction: "all"解决了我的问题:
var combo2 = new Ext.form.ComboBox({
triggerAction: "all",
store: [1,2,3],
renderTo: document.body
});
Run Code Online (Sandbox Code Playgroud)
ExtJS文档triggerAction并没有告诉我很多:
单击触发器时要执行的操作.使用'all'运行allQuery配置选项指定的查询(默认为'query')
我没有指定allQuery选项.实际上,我根本不想对服务器执行查询.
那triggerAction真的是什么呢?
"all"当我只想要一个简单的静态组合框时,它是否真的应该做到这一点?
我有一个数据绑定WPF comboxbox,我使用该SelectedValuePath属性根据对象的文本以外的东西选择一个选定的值.这可能最好用一个例子来解释:
<ComboBox ItemsSource="{Binding Path=Items}"
DisplayMemberPath="Name"
SelectedValuePath="Id"
SelectedValue="{Binding Path=SelectedItemId}"/>
Run Code Online (Sandbox Code Playgroud)
这个东西的datacontext看起来像这样:
DataContext = new MyDataContext
{
Items = {
new DataItem{ Name = "Jim", Id = 1 },
new DataItem{ Name = "Bob", Id = 2 },
},
SelectedItemId = -1,
};
Run Code Online (Sandbox Code Playgroud)
当我显示预先填充的数据时,这一切都很好,其中SelectedItemId匹配有效Item.Id.
问题是,在新项目的情况下,SelectedItemId未知的地方.WPF的作用是将组合框显示为空白.我不想要这个.我想禁止组合框中的空白项; 我希望它显示列表中的第一项.
这可能吗?我可以编写一些代码来明确地SelectedItemId预先设置,但由于UI的缺点,我不得不改变我的数据模型.
我试图在ComboBox(WPF)的DisplayMemberPath属性上使用StringFormat.但即使这是可能的,我也不知道.有人可以帮助我一些想法.
我想做这样的事情:
<ComboBox DisplayMemberPath="{Binding Path=MyDateField, StringFormat={}{0:dd/MM/yyyy}}" Name="CmbName" Width="120" />
Run Code Online (Sandbox Code Playgroud)
但它不起作用......
大家好
combobox ×10
c# ×5
wpf ×5
winforms ×4
.net ×2
data-binding ×2
autocomplete ×1
collections ×1
dynamic ×1
enums ×1
extjs ×1
javascript ×1
readonly ×1
selecteditem ×1
xaml ×1