我在线搜索过,但我无法找到问题的答案。可能是因为我不是100%熟悉XAML但是......
我希望能够通过代码设置组合框数据源。我做了什么。但是,此组合框选定值将更新以匹配列表视图中选择的值,并且在编辑组合框时,列表视图中的值也会更新。我认为这可以通过仅绑定来完成。我不想硬编码一些'解决问题。因为我已经找到了解决方案。
XAML代码:
<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#CODE:
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; }
}
答案 0 :(得分:0)
将List<string> Commands
替换为ObservableCollection<string> Commands
此外,您不应该反复分配集合。从中移除和/或添加项目。
所以你的代码变成了这个
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;
}
}
答案 1 :(得分:0)
找到了解决方案。
一个:
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}"
我必须设置ElementName和Path以匹配Listview。