c# - WPF 多个 ItemSource?

标签 c# wpf xaml itemssource

单个控件是否可以有多个 ItemSource?

给出以下代码:

<ComboBox Margin="137,101,169,183" ItemsSource="{Binding collection}"   SnapsToDevicePixels="True"            
 <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                <CheckBox Command="{Binding CheckCommand}" IsChecked="{Binding IsChecked}" Content="{Binding Name}"/>
                    <TextBlock Text="" />
                </StackPanel>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

ComboBox DataTemplate 中的 TextBlock 需要来自 VM 中除 ComboBox 之外的另一个属性的数据。如何实现这一目标?

谢谢。

最佳答案

您可以使用RelativeSource -FindAncestor 到达可视化树并获取不同的 DataContext。

例如(假设该命令是您想要的):

Command=”{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}, Path=CheckCommand}”

This也应该作为一个很好的资源。

编辑:拼写错误和资源。

关于c# - WPF 多个 ItemSource?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6296634/

相关文章:

c# - 刷新A gridview数据

c# - WPF 3D - 应用转换,更改基础对象值

c# - 如何在wpf treeview中显示所有直接和间接子成员的数量

c# - 使用反射的工厂模式 - 使用与每个类关联的静态键进行类注册

c# - 为什么 Visual Studio 项目仅限于一种语言?

c# - ASP.NET MVC 3 RC - Razor "View"属性

c# - 使用 wpf 检测窗口上任何位置的文本框

c# - 异步/等待未保留同步上下文?

c# - 列表框延伸到页面之外

c# - "ListViewItemPlaceholderBackgroundThemeBrush"位于何处?