C# Caliburn.Micro ItemsSource未绑定到当前viewmodel,而是绑定到父级
我正在为WPF使用Caliburn.Micro(使用VS2012,目标是.NET4.5.1) 我在将itemsSource绑定到ComboBox时遇到了问题(但我发现在我的例子中,其他具有itemsSource属性的控件(如ListBox)也会出现这种情况) 我使用SimpleContainer(IoC)创建的视图模型嵌套视图(用户控件) 我的问题是: 组合框中填充的项目不是来自其视图模型(LanguageSelectionViewModel),而是来自父视图视图模型(TopViewModel) 另外,当我从父视图模型中删除items集合时,我的组合框是空的 代码: MainWindowView.xaml:C# Caliburn.Micro ItemsSource未绑定到当前viewmodel,而是绑定到父级,c#,wpf,xaml,data-binding,caliburn.micro,C#,Wpf,Xaml,Data Binding,Caliburn.micro,我正在为WPF使用Caliburn.Micro(使用VS2012,目标是.NET4.5.1) 我在将itemsSource绑定到ComboBox时遇到了问题(但我发现在我的例子中,其他具有itemsSource属性的控件(如ListBox)也会出现这种情况) 我使用SimpleContainer(IoC)创建的视图模型嵌套视图(用户控件) 我的问题是: 组合框中填充的项目不是来自其视图模型(LanguageSelectionViewModel),而是来自父视图视图模型(TopViewModel)
<Window
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300"
d:DataContext="{d:DesignInstance d:Type=mainWindow:MainWindowViewModel}"
>
<Grid>
<top:TopView
HorizontalAlignment="Stretch"
cal:Bind.Model="{Binding TopVM}"
/>
</Grid>
</Window>
<UserControl>
<StackPanel Orientation="Horizontal">
<languageSelection:LanguageSelectionView cal:Bind.Model="{Binding LanguageSelectionVM}"/>
</StackPanel>
</UserControl>
<UserControl>
<StackPanel Orientation="Vertical">
<ComboBox ItemsSource="{Binding Items}"/>
</StackPanel>
</UserControl>
TopView.xaml:
<Window
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300"
d:DataContext="{d:DesignInstance d:Type=mainWindow:MainWindowViewModel}"
>
<Grid>
<top:TopView
HorizontalAlignment="Stretch"
cal:Bind.Model="{Binding TopVM}"
/>
</Grid>
</Window>
<UserControl>
<StackPanel Orientation="Horizontal">
<languageSelection:LanguageSelectionView cal:Bind.Model="{Binding LanguageSelectionVM}"/>
</StackPanel>
</UserControl>
<UserControl>
<StackPanel Orientation="Vertical">
<ComboBox ItemsSource="{Binding Items}"/>
</StackPanel>
</UserControl>
TopViewModel.cs:
public class TopViewModel : Screen
{
private LanguageSelectionViewModel _languageSelectionVM;
public LanguageSelectionViewModel LanguageSelectionVM
{
get { return _languageSelectionVM; }
set
{
_languageSelectionVM = value;
NotifyOfPropertyChange(() => LanguageSelectionVM);
}
}
public TopViewModel(ClockViewModel clockVm, LanguageSelectionViewModel languageSelectionVM)
{
this.Items = new ObservableCollection<string>() { "a", "a", "a" };
LanguageSelectionVM = languageSelectionVM;
LanguageSelectionVM.ConductWith(this);
}
private ObservableCollection<string> _items;
public ObservableCollection<string> Items
{
get { return _items; }
set
{
_items = value;
NotifyOfPropertyChange(() => Items);
}
}
}
public class LanguageSelectionViewModel : Screen
{
private ObservableCollection<string> _items;
public ObservableCollection<string> Items
{
get { return _items; }
set
{
_items = value;
NotifyOfPropertyChange(() => Items);
}
}
public LanguageSelectionViewModel()
{
this.Items = new ObservableCollection<string>() { "1", "a" };
}
}
公共类TopViewModel:屏幕
{
private LanguageSelectionViewModel _languageSelectionVM;
公共语言选择视图模型语言选择VM
{
获取{return\u languageSelectionVM;}
设置
{
_languageSelectionVM=值;
NotifyOfPropertyChange(()=>LanguageSelectionVM);
}
}
公共TopViewModel(ClockViewModel clockVm,LanguageSelectionViewModel languageSelectionVM)
{
this.Items=newobserveCollection(){“a”、“a”、“a”};
LanguageSelectionVM=LanguageSelectionVM;
LanguageSelectionVM.ConductWith(this);
}
私人可观测收集项目;
公共可观测收集项目
{
获取{return\u items;}
设置
{
_项目=价值;
财产变更通知(()=>项);
}
}
}
LanguageSelectionView.xaml:
<Window
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300"
d:DataContext="{d:DesignInstance d:Type=mainWindow:MainWindowViewModel}"
>
<Grid>
<top:TopView
HorizontalAlignment="Stretch"
cal:Bind.Model="{Binding TopVM}"
/>
</Grid>
</Window>
<UserControl>
<StackPanel Orientation="Horizontal">
<languageSelection:LanguageSelectionView cal:Bind.Model="{Binding LanguageSelectionVM}"/>
</StackPanel>
</UserControl>
<UserControl>
<StackPanel Orientation="Vertical">
<ComboBox ItemsSource="{Binding Items}"/>
</StackPanel>
</UserControl>
LanguageSelectionViewModel.cs:
public class TopViewModel : Screen
{
private LanguageSelectionViewModel _languageSelectionVM;
public LanguageSelectionViewModel LanguageSelectionVM
{
get { return _languageSelectionVM; }
set
{
_languageSelectionVM = value;
NotifyOfPropertyChange(() => LanguageSelectionVM);
}
}
public TopViewModel(ClockViewModel clockVm, LanguageSelectionViewModel languageSelectionVM)
{
this.Items = new ObservableCollection<string>() { "a", "a", "a" };
LanguageSelectionVM = languageSelectionVM;
LanguageSelectionVM.ConductWith(this);
}
private ObservableCollection<string> _items;
public ObservableCollection<string> Items
{
get { return _items; }
set
{
_items = value;
NotifyOfPropertyChange(() => Items);
}
}
}
public class LanguageSelectionViewModel : Screen
{
private ObservableCollection<string> _items;
public ObservableCollection<string> Items
{
get { return _items; }
set
{
_items = value;
NotifyOfPropertyChange(() => Items);
}
}
public LanguageSelectionViewModel()
{
this.Items = new ObservableCollection<string>() { "1", "a" };
}
}
公共类语言SelectionViewModel:屏幕
{
私人可观测收集项目;
公共可观测收集项目
{
获取{return\u items;}
设置
{
_项目=价值;
财产变更通知(()=>项);
}
}
公共语言SelectionViewModel()
{
this.Items=newobserveCollection(){“1”,“a”};
}
}
我后来也尝试填充此集合,但没有成功:
protected override void OnViewReady(object view)
{
base.OnViewReady(view);
this.Items = new ObservableCollection<string>() { "1", "a" };
Refresh();
}
受保护的覆盖无效OnViewReady(对象视图)
{
base.OnViewReady(视图);
this.Items=newobserveCollection(){“1”,“a”};
刷新();
}
DataContext似乎还可以,因为绑定到textbox
<TextBlock Text="{Binding TestString}"/>
工作正常。我发现ComboBox是唯一一个将DataContext设置为父视图模型而不是正确视图模型的控件 它通过以下方式强制工作:
<ComboBox
DataContext="{Binding}"
ItemsSource="{Binding Items}" >
但问题仍然是——为什么?这是Caliburn.Micro的错误或功能?好的,谜团解决了
而不是像这样嵌套控件:
<Grid>
<top:TopView
cal:Bind.Model="{Binding TopVM}" />
</Grid>
我应该写:
<Grid>
<ContentControl
cal:View.Model="{Binding TopVM}" />
</Grid>
并且没有必要强制使用DataContext