這是我的ViewModel:DataTemplateSelector /綁定不起作用
class MainViewModel : ViewModelBase
{
private CalculatorViewModel calc;
public CalculatorViewModel Calculator {get {return calc; }set {calc = value; RaisePropertyChanged("Calculator");}}
}
class CalculatorViewModel : ViewModelBase
{
private CalculatorMode mode;
public CalculatorMode Mode {get {return mode;}set{mode=value; RaisePropertyChanged("CalculatorMode");}}
public CalculatorViewModel()
{
Mode = new HexMode();
}
}
abstract class CalculatorMode : ViewModelBase
{
abstract int Calculate(...);
}
class HexMode : CalculatorMode
{
public int Calculate(...) { ...}
}
這是我的主窗口(一個MainViewModel實例被設置爲Windows'的DataContext):
<ad:DockingManager >
<ad:DockingManager.LayoutItemTemplateSelector>
<vm:PanesTemplateSelector>
<vm:PanesTemplateSelector.FileViewTemplate>
<DataTemplate>
<h:MyDocument />
</DataTemplate>
</vm:PanesTemplateSelector.FileViewTemplate>
<vm:PanesTemplateSelector.CalculatorViewTemplate>
<DataTemplate>
<w:Calculator />
</DataTemplate>
</vm:PanesTemplateSelector.CalculatorViewTemplate>
</vm:PanesTemplateSelector>
</ad:DockingManager.LayoutItemTemplateSelector>
</ad:DockingManager>
的代碼PanesTemplateSelector級:
public DataTemplate FileViewTemplate
{
get;
set;
}
public DataTemplate CalculatorViewTemplate
{
get;
set;
}
public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
{
if (item is DocumentViewModel)
return FileViewTemplate;
if (item is CalcualtorViewModel)
return CalculatorViewTemplate;
return base.SelectTemplate(item, container);
}
計算器控制的內容:
<Grid Name="main">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<ContentControl Name="content">
<ContentControl.ContentTemplateSelector>
<vm:CalculatorTemplateSelector>
<vm:CalculatorTemplateSelector.DefaultView>
<DataTemplate>
<TextBlock Text="Dummy for normal view" Foreground="Wheat" />
</DataTemplate>
</vm:CalculatorTemplateSelector.DefaultView>
<vm:CalculatorTemplateSelector.HexView>
<DataTemplate>
<TextBlock Text="Dummy für Hexview" Foreground="Gray"/>
</DataTemplate>
</vm:CalculatorTemplateSelector.HexView>
</vm:CalculatorTemplateSelector>
</ContentControl.ContentTemplateSelector>
</ContentControl>
</Grid>
的CalculatorTemplateSelector級的代碼:
public DataTemplate HexView
{
get;
set;
}
public DataTemplate DefaultView
{
get;
set;
}
public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
{
CalculatorViewModel cjmv = (CalculatorViewModel)item;
if (cjmv != null)
{
if (cjmv.Mode is HexMode)
return HexView;
return DefaultView;
}
return base.SelectTemplate(item, container);
}
第一TemplateSelector正在工作。選擇了正確的模板,我可以看到計算器控制。但是Calculator-Control中的TemplateSelector不起作用。在CalculatorTemplateSelector.SelectTemplate中,項目參數始終爲空。但我需要那裏的CalculatorViewModel實例。我想我的問題是一個有約束力的問題,但我沒有找到解決方案。有任何想法嗎?
謝爾頓·庫珀的通知..問一個問題!哦,我的日子!艾米如何? :P 無論如何,因爲你的問題可能是回答..你可能想要更改此行'CalculatorViewModel cjmv =(CalculatorViewModel)項目;'到'CalculatorViewModel cjmv =項目作爲CalculatorViewModel;'否則可能會拋出異常.. –
+1謝爾頓庫珀問一個問題! – franssu