我有下一个组合框:
this.comboBoxProd.Enabled = false;
this.comboBoxProd.FormattingEnabled = true;
this.comboBoxProd.Items.AddRange(new object[] {
"Cameras",
"------------",
" Digital IXUS 850 IS ",
" Digital IXUS 900 Ti ",
" Digital IXUS 75 -NEW- ",
" Digital IXUS 70 -NEW- ", etc.
Run Code Online (Sandbox Code Playgroud)
我想更改它并从数据库中获取值。我的数据库名称是 bd1.mdb,在表 Cameras 中有以下字段:CamID、Cameras、Warranty、Year。我只对“相机”领域感兴趣。谢谢!
有什么方法可以将 myList<string>
与 a同步ComboBox
?
我想要的是我的 ComboBox,它根据列表的变化自动更新它的内容。
我已经尝试使用该ComboBox.DataSource
属性,但这不会更新 ComboBox,它只会填充一次,仅此而已,所以......
我在我的应用程序中使用了一个组合框,我用这样的类填充它:
namespace Foo.Bar{
public class Item
{
public string lastName;
public string firstName;
public Foo theMeatyPart;
}
}
Run Code Online (Sandbox Code Playgroud)
我可以使用 itemTamplate 用“lastName, firstName”填充下拉列表,但选定的值显示为“Foo.Bar.Item”。如何将相同的模板应用于 selectedItem 并且在不覆盖 Item 的 ToString 方法的情况下使搜索功能正常工作?
这是xaml:
<Style x:Key="SearchComboStyle" TargetType="ComboBox">
<Style.Setters>
<Setter Property="Width" Value="150"></Setter>
</Style.Setters>
</Style>
<DataTemplate x:Key="SearchComboItemTemplate" >
<TextBlock DataContext="{Binding}">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}, {1}">
<Binding Path="lastName"/>
<Binding Path="firstName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
<ComboBox ItemTemplate="{StaticResource SearchComboItemTemplate}" Style="{StaticResource SearchComboStyle}"
ItemsSource="{Binding Path=PhysiciansList, RelativeSource={RelativeSource AncestorType=local:ExamViewerControl, AncestorLevel=1}}" IsTextSearchEnabled="True" IsTextSearchCaseSensitive="False" IsEditable="True" TextSearch.TextPath="Person.LastName" />
Run Code Online (Sandbox Code Playgroud) 我想(在 C# 中)用枚举的可接受值填充组合框的可接受值列表System.IO.Ports.Parity
。为此,我创建了一个集合:
public class theParitySource : ObservableCollection<Parity>
{
public theParitySource()
{
Array parities = System.Enum.GetValues( typeof( Parity ) );
foreach (Parity p in parities) this.Add(p);
}
}
Run Code Online (Sandbox Code Playgroud)
(顺便说一句:这个初始化有单行吗?)并将其作为组合框的数据上下文:
...
xmlns:local="clr-namespace:myNamespace"
...
<ComboBox ...>
<ComboBox.DataContext>
<local:theParitySource />
</ComboBox.DataContext>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
然而,组合框仍然是空的(它显示为空,但似乎具有正确的长度),即使我可以在调试器中看到如何theParitySource
填充。这种方法确实适用于另一个组合框(即使在同一个类中),我为波特率执行此操作。我用整数值初始化,所以我想这与我在这里使用枚举的事实有某种关系,但我不知道可能是什么原因。任何指针?我需要写一个转换器吗?
(当然我可以通过从枚举创建一个字符串列表来解决这个问题,但这会有点不愉快......)
编辑:实际上我更喜欢在 XAML 中完成所有这些。有没有一种简单的方法可以做到这一点?
我无法理解组合框的行为。
我有一个编辑视图来编辑现有的订单数据。这是我的部分订单数据的 VM:
public class OrderDataViewModel : ViewModelBase, IOrderDataViewModel
{
private readonly ICustomersListProviderService _customersListProviderService;
private readonly Order _order;
public OrderDataViewModel(Order order, ICustomersListProviderService customersListProviderService)
{
Assign.IfNotNull(ref _order, order);
Assign.IfNotNull(ref _customersListProviderService, customersListProviderService);
}
public DateTime CreationDate
{
get { return _order.CreationDate ?? (_order.CreationDate = DateTime.Now).Value; }
set
{
if (_order.CreationDate == value) return;
_order.CreationDate = value;
OnPropertyChanged();
}
}
public Customer Customer
{
get { return _order.Customer; }
set
{
if (_order.Customer == value) return;
_order.Customer = value;
OnPropertyChanged();
}
}
private …
Run Code Online (Sandbox Code Playgroud) 我正在使用 Visual Studio 2010 和 C# 创建一个带有组合框的 Windows 窗体,该窗体应包含员工姓名首字母。在过去的几天里,我一直在搜索我能找到的所有解决方案,但仍然无法填充我的组合框。
这是我目前所拥有的:
public static void FillComboBox(string Query, System.Windows.Forms.ComboBox LoggedByBox)
{
using (var CONN = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\Documents\\Service Request Application\\bin\\Debug\\servicereq1.mdb"))
{
CONN.Open();
DataTable dt = new DataTable();
try
{
OleDbCommand cmd = new OleDbCommand(Query, CONN);
OleDbDataReader myReader = cmd.ExecuteReader();
dt.Load(myReader);
}
catch (OleDbException e)
{
Console.WriteLine(e.ToString());
Console.ReadLine();
return;
}
LoggedByBox.DataSource = dt;
LoggedByBox.ValueMember = "ID";
LoggedByBox.DisplayMember = "Initials";
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在表单加载时调用它
private void Form1_Load(object sender, EventArgs e)
{
FillComboBox("select ID, Initials from [Fixers …
Run Code Online (Sandbox Code Playgroud) 我有点绝望,因为昨天它可以工作,但今天这不再有效,让我解释一下,我正在尝试做什么:
我想设置DataSource
的ComboBox
所有枚举值在一个特定的枚举。就像这样:
cmbType.DataSource = m_addViewPresenter.Types;
Run Code Online (Sandbox Code Playgroud)
其中 Types 是 aBindingList
并且正在像这样初始化:
public BindingList<MyEnum> Types
{
get { return m_types; }
set
{
m_types = value;
OnPropertyChanged();
}
}
[ImportingConstructor]
public AddViewPresenter()
{
Types = new BindingList<MyEnum>(
Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>().ToList());
}
Run Code Online (Sandbox Code Playgroud)
此外,我想将选择的当前项目绑定到一个属性。由于ComboBox.SelectedItem
不触发 -INotifyProperty
事件,我使用ComboBox.SelectedValue
.
cmbType.Bind(m_addViewPresenter, c => c.SelectedValue, m => m.SelectedValue);
public static void Bind<TComponent, T>(
this TComponent component, T value,
Expression<Func<TComponent, object>> controlProperty,
Expression<Func<T, object>> modelProperty)
where TComponent : IBindableComponent
where T …
Run Code Online (Sandbox Code Playgroud) 我有一个标签字段,我希望我的用户能够向它添加新值。我可以通过设置来实现这一点forceSelection: false
。用户可以在标签字段中输入他们的新条目。当他们完成输入当前条目时,他们可以按逗号,它将被添加。
问题是逗号键不是非常直观,更重要的是用户无法添加本身包含逗号的条目。无论如何我可以将此功能重新分配给回车键吗?
jsFiddle在这里:链接
我需要让用户选择一个月和一年。通常,他们会想要最后一个月(例如,如果是 2015 年 12 月 18 日,他们通常会选择 2015 年 11 月)。所以我希望这些值是组合框中预先选择的值。
我怎样才能做到这一点(在 C# 中),并且一旦越过卢比孔河(幼发拉底河?)到 2016 年 1 月,它也将是“2015 年 12 月”?
我也希望它不会增加2016,直到它是2016(依此类推,随着岁月的流逝),并开始从预定一年(不0年或1970年或类似的东西,不一定)。
我的问题如下,
为了这个问题,我在一个新项目中重现了这个问题.
假设我有一个带有组合框的应用程序,那里可能有一个或多个项目.我希望当用户点击组合框中的某个项目时会发生"某事".
我制作了以下代码:
obsvList.add("item1");
cbTest.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Item clicked");
}
});
Run Code Online (Sandbox Code Playgroud)
这在应用程序启动并且第一次选择项目时有效.当组合框中有2个或更多项目时(当用户单击项目1,然后单击项目2,然后单击项目1时),这也适用
但是我的问题是,当组合框中只有1个项目时,让我们说"item1".然后用户重新打开组合框并再次单击"item1",然后它将不会重做动作.
只有在单击"新建"项目时,它才会打印"项目单击"行.
我希望它能说清楚我遇到的问题是什么,如果没有请求澄清,我会在需要的地方给出.
提前致谢!