MVVM自定义ComboBox绑定 [英] MVVM Custom ComboBox Binding
问题描述
大家好,
我的StocjkQueryView.xaml中具有以下组合框:
I have the following ComboBox in my StocjkQueryView.xaml:
<ComboBox Grid.Column="1" Grid.Row="0" ItemsSource="{Binding allActions}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected}"
Width="20" />
<TextBlock Text="{Binding Action}"
Width="100" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我的Resources.xaml中也有以下代码:
I also have this code in my Resources.xaml:
<DataTemplate DataType="{x:Type vm:StockQueryViewModel}">
<vw:StockQueryView />
</DataTemplate>
然后我有一个ObservableCollection类,其中包含一个IsSelected布尔值和一个用于从服务调用进行绑定的字符串Action:
I then have an ObservableCollection class that contains an IsSelected bool and a string Action for binding from a service call:
class ObservableActions : ObservableCollection<SelectableAction>
{
public ObservableActions()
{
foreach (var item in WCF_Actions.scs.getListOfActions(false))
{
Add(new SelectableAction(false, item.ActionDescription));
}
}
}
class SelectableAction
{
public SelectableAction(bool sel, string act)
{
this.isSelected = sel;
this.action = act;
}
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set { isSelected = value; }
}
private string action;
public string Action
{
get { return action; }
set { action = value; }
}
}
然后,我尝试绑定到以下列表,该列表包含我希望在ComboBox中呈现的动作:
I'm then trying to bind to the following list that contains the actions I wish to present in the ComboBox:
public ObservableActions allActions = new ObservableActions();
此绑定如何/为什么不起作用?我在这里不明白什么?
How/Why is this binding not working? What am I not understanding here?
谢谢,
臂架
推荐答案
嘿臂架,
您能为您的UserControl容器发布XAML吗?如果我不得不猜测,我想说您需要使用Relative绑定将ComboBox绑定到包含它的UserControl的DataContext上. 很难猜测.
Can you please post the XAML for the container of your UserControl? If I had to guess, I'd say you need to bind your ComboBox to the DataContext of the UserControl that contains it using a Relative binding...that said, without seeing your code it's tough to guess.
Aj
这篇关于MVVM自定义ComboBox绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!