我有一个对某些人来说似乎非常简单的 WPF 问题。我对 WPF 并不陌生,但也不是专业人士。
尝试将内容控件绑定到树形视图的选定项。搜索了许多帖子,这似乎对其他人有用,但无法让数据模板根据数据类型显示某些内容。
<ContentControl Grid.Column="1" Grid.RowSpan="3" Grid.ColumnSpan="2" DataContext="{Binding SelectedItem, ElementName=DataManagerTreeView, Mode=OneWay}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type daq:DAQChartChannel}">
<StackPanel Orientation="Vertical">
<Label>Name</Label>
<Label Content="{Binding Name}"></Label>
<Label>AxisX</Label>
<Label Content="{Binding AxisX}"></Label>
<Label>AxisY</Label>
<Label Content="{Binding AxisY}"></Label>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type daq:DAQChart}">
<StackPanel Orientation="Vertical">
<Label>Name</Label>
<Label Content="{Binding Name}"></Label>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type daq:DAQChartsCollection}">
<StackPanel Orientation="Vertical">
<Label>Name</Label>
<Label Content="{Binding Name}"></Label>
</StackPanel>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
如果将单个堆栈面板直接放在我的 ContentControl 中,它会正确显示当前选定项目的数据(至少是所有项目类型中唯一存在的名称),因此绑定是正确的。但是当我尝试使用 ContentControl.Resources 根据带有 DataTemplates 的源类型显示数据时,我没有得到任何输出。我这里遗漏了什么吗?
除了使用 DataTemplateSelector 之外,没有其他解决方法吗?感谢您的帮助