C# WPF组合框绑定不';行不通
我正在学习WPF,我有一个问题。我试图在加载表单时初始化组合框数据,但什么也没有得到。这是我的密码: xaml:C# WPF组合框绑定不';行不通,c#,wpf,combobox,C#,Wpf,Combobox,我正在学习WPF,我有一个问题。我试图在加载表单时初始化组合框数据,但什么也没有得到。这是我的密码: xaml: <Menu x:Name="mn_MainMenu" HorizontalAlignment="Left" Height="35" VerticalAlignment="Top" Width="518"> <ComboBox Name="cbm_Menu" SelectedIndex="0" Width="340"> </ComboBox
<Menu x:Name="mn_MainMenu" HorizontalAlignment="Left" Height="35" VerticalAlignment="Top" Width="518">
<ComboBox Name="cbm_Menu" SelectedIndex="0" Width="340">
</ComboBox>
<Button HorizontalAlignment="Right" Content="Demo" Width="50"/>
<Button HorizontalAlignment="Right" Content="Source Code" Width="80"/>
</Menu>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitCbxData(StaticVariable.ComboBoxData);
}
private void InitCbxData(string[] initStrings)
{
var comboBoxData = new ComboBoxData(initStrings);
this.cbm_Menu.ItemsSource = comboBoxData.Items;
this.cbm_Menu.DisplayMemberPath = "Name";
this.cbm_Menu.SelectedValuePath = "Id";
this.cbm_Menu.SelectedValue = "1";
}
}
这里有一个screeshot,当我调试时:
更新:
public class ComboBoxData:ItemCollection
{
public ComboBoxData(params string[] initStrings) : base(initStrings) { }
public ComboBoxData(string initString, char[] delimiters) : base(initString, delimiters) { }
}
public class Item
{
public int Id;
public string Name;
public Item(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
public class ItemCollection : IEnumerable
{
public Item[] Items;
public int Ctr = 0;
public ItemCollection(params string[] initStrings)
{
this.Items = new Item[initStrings.Count()];
foreach (var s in initStrings)
{
this.Items[Ctr] = new Item(Ctr++, s);
}
}
public ItemCollection(string initString, char[] delimiters)
{
string[] stringElements = initString.Split(delimiters);
foreach (var s in stringElements)
{
this.Items[Ctr++] = new Item(Ctr, s);
}
}
public IEnumerator GetEnumerator()
{
return new ItemCollectionEnumerator(this);
}
}
public class ItemCollectionEnumerator : IEnumerator
{
public int position = -1;
public ItemCollection itemCollection;
public ItemCollectionEnumerator(ItemCollection itemCollection)
{
this.itemCollection = itemCollection;
}
public object Current
{
get { return itemCollection.Items[position]; }
}
public bool MoveNext()
{
if (position < itemCollection.Items.Length - 1)
{
position++;
return true;
}
else
{
return false;
}
}
public void Reset()
{
position = -1;
}
}
公共类ComboBoxData:ItemCollection
{
公共ComboBoxData(参数字符串[]initStrings):基(initStrings){}
公共ComboBoxData(字符串initString,字符[]分隔符):基(initString,分隔符){}
}
公共类项目
{
公共int Id;
公共字符串名称;
公共项(整数id、字符串名称)
{
这个.Id=Id;
this.Name=Name;
}
}
公共类ItemCollection:IEnumerable
{
公共物品【】项;
公共int Ctr=0;
公共ItemCollection(参数字符串[]initStrings)
{
this.Items=新项[initStrings.Count()];
foreach(initStrings中的var s)
{
this.Items[Ctr]=新项(Ctr++,s);
}
}
公共ItemCollection(string initString,char[]分隔符)
{
string[]stringElements=initString.Split(分隔符);
foreach(stringElements中的变量s)
{
此.Items[Ctr++]=新项(Ctr,s);
}
}
公共IEnumerator GetEnumerator()
{
返回新的ItemCollectionEnumerator(此);
}
}
公共类ItemCollectionEnumerator:IEnumerator
{
公共int位置=-1;
公共物品收集;
公共ItemCollectionEnumerator(ItemCollection ItemCollection)
{
this.itemCollection=itemCollection;
}
公共对象流
{
获取{return itemCollection.Items[position];}
}
公共图书馆
{
if(位置
我想这是行不通的。首先,您会收到一个字符串数组,因此,对于de DisplayMemberPath和SelectedValuePath,没有可用的“名称”或“Id”
在我看来,你唯一要做的就是
this.cbm_Menu.ItemsSource = initstrings;
DisplayMemberPath和selectedValuePath应用于ID和名称作为属性的对象…(对于您的场景…问题已解决
只需要在Item类中将公共字段修改为属性。我真的不知道为什么,但它奏效了
修改为:
public class Item
{
public int Id {get;set;}
public string Name {get;set;}
public Item(int id, string name)
{
this.Id = id;
this.Name = name;
}
}
使用WPF,您不应该在UI中放入代码。查看MVVM模式,WPF就是这样编程的。使用MVVM真的很酷,不使用它会让人觉得你每一步都在与WPF抗争。我同意@nvoigt。我想说的是你没有学习WPF。。。您正试图以WinForms的方式使用WPF。。。WPF不是WinForms。我建议你停止做你正在做的事情,在继续之前看看MSDN上的页面。WPF和WinForms之间存在巨大的差异,在不知道如何编写WPF的情况下尝试编写WPF将是浪费您的时间。感谢@Sheridan。正如你所说,我真的试图以WinForms的方式使用WPF…@Sheridan。事实上,我没有遵循任何WPF教程。我只是创建了一个新的WPF项目并开始编码。我更喜欢探索。在我完成这个小项目后,我会看看课程,看看我可以改进或修改什么(可能完全…)。虽然我对你的“寻找并发现”方法表示赞赏,但我不能强调你不会以这种方式计算WPF。它中的几乎所有内容都与其他编程语言截然不同,以至于您永远无法找到所需的内容。例如,如果不实现,就无法编写有效的WPF,但我可以看出您没有发现这一点。祝你好运。嗨,塞克斯塔,谢谢你回答我的问题。我收到一个字符串数组,是用来初始化comboBoxData的。这是一个IEnumerable类。我将编辑我的文章,让你了解更多。绑定只适用于属性,不适用于字段,我想这就是原因。
public class Item
{
public int Id {get;set;}
public string Name {get;set;}
public Item(int id, string name)
{
this.Id = id;
this.Name = name;
}
}