C#数据绑定问题 [英] C# data binding question
问题描述
大家好我正在尝试从我制作的US_State类的静态列表中加载一个组合框。列表会在我的WPF窗口中填充所有正确的数据,但如果我放入ComboboxState.ItemsSource = Get_States,我的组合框会加载;在我的窗口代码后面,但我想我可以通过绑定加载组合框。这是我的代码我有
public partial class ProfileScreen:Window
{ // < span class =code-comment>列表填充正确的数据
public 列表< US_State> Get_States = StatesArray.Get_States;
}
// xaml
< ComboBox x:名称= ComboboxState Grid.Column = 1 Grid.Row = 2 VerticalAlignment = 中心宽度= 120 HorizontalAlignment = Left ToolTip = 输入您的州。
ItemsSource = {Binding Get_States,RelativeSource = {RelativeSource AncestorType = Window,Mode = FindAncestor},UpdateSourceTrigger = PropertyChanged}
DisplayMem berPath = 名称 SelectedValuePath = 缩写 />
Us_State中的代码使用Inotify接口和static class只创建一个List< us_state>并加载它。
我尝试过:
数据绑定不要工作,但这适用于codebehind
< ComboBox x:名称 = ComboboxState 网格.Column = 1 Grid.Row = 2 VerticalAlignment = 中心 Widt h = 120 HorizontalAlignment = 左 工具提示 = 输入您的州。
< span class =code-attribute> ItemsSource = {Binding Get_States} SelectedItem = {Binding SimpleStringProperty, 模式 = TwoWay} , UpdateSourceTrigger = PropertyChanged}
DisplayMemberPath = 名称 SelectedValuePath = 缩写 / >
试一试,看看它是否有效。如果没有,请告诉我。
public List< US_State> Get_States = StatesArray.Get_States;
WPF数据绑定仅适用于属性。你已经声明了一个字段,它不起作用。
如果在Visual Studio中检查输出窗口,你会看到一个数据绑定错误告诉你该物业不存在。
尝试将您的油田更改为物业:
公开列表< US_State> Get_States => StatesArray.Get_States;
Hello all I’m trying to load a combobox from a static list of a class US_State I made. The list gets populated in my WPF window with all the correct data but my combobox loads if I put ComboboxState.ItemsSource = Get_States; in my window code behind but I thought I could load the combobox by just binding. Here is my code I have
public partial class ProfileScreen : Window
{ //List populates with correct data
public List<US_State> Get_States = StatesArray.Get_States;
}
//xaml
<ComboBox x:Name="ComboboxState" Grid.Column="1" Grid.Row="2" VerticalAlignment="Center" Width="120" HorizontalAlignment="Left" ToolTip="Enter your state."
ItemsSource="{Binding Get_States, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Name" SelectedValuePath="Abbreviation"/>
My code in Us_State uses Inotify interface and static class just creates a List<us_state> and loads it.
What I have tried:
databinding dont work but this works in codebehind
<ComboBox x:Name="ComboboxState" Grid.Column="1" Grid.Row="2" VerticalAlignment="Center" Width="120" HorizontalAlignment="Left" ToolTip="Enter your state." ItemsSource="{Binding Get_States} SelectedItem="{Binding SimpleStringProperty, Mode=TwoWay}" , UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name" SelectedValuePath="Abbreviation"/>
Try this and see if it will work. Let me know if did not.
public List<US_State> Get_States = StatesArray.Get_States;
WPF data binding only works with properties. You've declared a field, which won't work.
If you check the output window in Visual Studio, you'll see a data binding error telling you that the property doesn't exist.
Try changing your field to a property:
public List<US_State> Get_States => StatesArray.Get_States;
这篇关于C#数据绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!