C# Combobox和DisplayMemberBinding值
我已经在网上搜索过了,但我找不到一个可靠的答案来回答我的问题。这可能是因为我不是100%熟悉XAML,但 我希望能够通过代码设置一个组合框数据源。我已经做到了。但是,此comboboxs selected值会更新以匹配listview中选择的值,并且在编辑ComboxBox时,listview中的值也会更新。我以为这只能通过装订来完成。我不想硬编码一些“变通”。因为我已经找到了解决办法 XAML代码:C# Combobox和DisplayMemberBinding值,c#,wpf,xaml,listview,combobox,C#,Wpf,Xaml,Listview,Combobox,我已经在网上搜索过了,但我找不到一个可靠的答案来回答我的问题。这可能是因为我不是100%熟悉XAML,但 我希望能够通过代码设置一个组合框数据源。我已经做到了。但是,此comboboxs selected值会更新以匹配listview中选择的值,并且在编辑ComboxBox时,listview中的值也会更新。我以为这只能通过装订来完成。我不想硬编码一些“变通”。因为我已经找到了解决办法 XAML代码: <ListView Name="ActionListView" ItemsSource=
<ListView Name="ActionListView" ItemsSource="{Binding Actions, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Path=FirstListViewSelectedItem, Mode=TwoWay}">
<ListView.View>
<GridView>
<GridViewColumn Header="Command" DisplayMemberBinding="{Binding Command}" />
</GridView>
</ListView.View>
</ListView>
<ComboBox IsEditable="True" Name="CommandCB" ItemsSource="{Binding Commands}" SelectedValue="{Binding Path=Command, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
C代码:
public partial class DetailsView : UserControl, INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private ObservableCollection<ActionObject> myActions;
public ObservableCollection<ActionObject> Actions
{
get { return myActions; }
set
{
if (myActions != value)
{
myActions = value;
OnPropertyChanged("Actions");
}
}
}
private List<string> myCommands;
public List<string> Commands
{
get { return myCommands; }
set
{
if (myCommands != value)
{
myCommands = value;
OnPropertyChanged("Commands");
}
}
}
private string myCommand;
public string Command
{
get { return myCommand; }
set
{
if (value != myCommand)
{
myCommand = value;
OnPropertyChanged("Command");
}
}
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
public DetailsView()
{
myCommands = GetCommandObjects().Select(x => x.Name).ToList();
myActions = new ObservableCollection<ActionObject>();
InitializeComponent();
DataContext = this;
}
private List<CommandObject> GetCommandObjects()
{
using (StreamReader r = new StreamReader("commands.json"))
{
return JsonConvert.DeserializeObject<List<CommandObject>>(r.ReadToEnd());
}
}
private void MenuItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (ActionListView.SelectedIndex != -1)
{
if (ActionListView.SelectedItem == null)
return;
ActionObject _action = myActions.ElementAt(ActionListView.SelectedIndex);
Command = _action.Command;
}
}
}
public class ActionObject
{
public string Command { get; set; }
public string Target { get; set; }
public string Value { get; set; }
public string Comment { get; set; }
}
将列表命令替换为ObservableCollection命令
此外,您不应该一次又一次地分配集合。从中删除和/或添加项目
所以你的代码变成了这样
private ObservableCollection<ActionObject> myActions;
public ObservableCollection<ActionObject> Actions
{
get
{
if (myActions == null)
myActions = new ObservableCollection<ActionObject>();
return myActions;
}
}
private ObservableCollection<string> myCommands;
public ObservableCollection<string> Commands
{
get
{
if (myCommands == null)
myCommands = new ObservableCollection<string>();
return myCommands;
}
}
将列表命令替换为ObservableCollection命令
此外,您不应该一次又一次地分配集合。从中删除和/或添加项目
所以你的代码变成了这样
private ObservableCollection<ActionObject> myActions;
public ObservableCollection<ActionObject> Actions
{
get
{
if (myActions == null)
myActions = new ObservableCollection<ActionObject>();
return myActions;
}
}
private ObservableCollection<string> myCommands;
public ObservableCollection<string> Commands
{
get
{
if (myCommands == null)
myCommands = new ObservableCollection<string>();
return myCommands;
}
}
找到了解决办法
以前的:
ItemsSource="{Binding Commands}" SelectedValue="{Binding Path=Command, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
答复:
ItemsSource="{Binding Commands}" SelectedValue="{Binding ElementName=ActionListView, Path=SelectedItem.Command, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
我必须设置元素名和路径以匹配Listview。找到了解决方案
以前的:
ItemsSource="{Binding Commands}" SelectedValue="{Binding Path=Command, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
答复:
ItemsSource="{Binding Commands}" SelectedValue="{Binding ElementName=ActionListView, Path=SelectedItem.Command, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
我必须设置元素名和路径以匹配Listview。据我所知,我需要它是一个列表。除非有办法改变。我有一个来自JSON脚本的对象列表。json无法将列表转换为ObservableCollection。myCommand=GetCommandObjects.Selectx=>x.Name.ToList;您始终可以在myList命令中执行foreachvar项。Additem,请尝试添加属性而不是其备份字段。我已将代码更改为:'MyCommand=new ObservableCollectionGetCommandObjects.Selectx=>x.Name.ToList;'但是我不明白这是如何回答我的问题的?如果我们直接命中属性,“备份字段”有什么意义?您的VS是否支持此公共ObservableCollection命令{get;}=new ObservableCollection;如果是,那么像这样更改集合属性。据我所知,我需要它是一个列表。除非有办法改变。我有一个来自JSON脚本的对象列表。json无法将列表转换为ObservableCollection。myCommand=GetCommandObjects.Selectx=>x.Name.ToList;您始终可以在myList命令中执行foreachvar项。Additem,请尝试添加属性而不是其备份字段。我已将代码更改为:'MyCommand=new ObservableCollectionGetCommandObjects.Selectx=>x.Name.ToList;'但是我不明白这是如何回答我的问题的?如果我们直接命中属性,“备份字段”有什么意义?您的VS是否支持此公共ObservableCollection命令{get;}=new ObservableCollection;如果是,则像这样更改集合属性。