WPF 多个 ItemSource?
单个控件是否可以有多个 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 之外的另一个属性的数据。如何才能实现这一目标?
谢谢。
Is it possible to have multiple ItemSources for a single control?
Given the code below:
<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>
The TextBlock within the ComboBox DataTemplate requires data from another property within the VM than that of the ComboBox. How can this be achieved?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 RelativeSource-FindAncestor 到达可视化树并获取不同的数据上下文。
例如(假设该命令是您想要的):
这个 也应该作为一个很好的资源。
编辑:拼写错误和资源。
You can use RelativeSource-FindAncestor to reach up the visual tree and grab a different DataContext.
For example (assuming the command is what you want):
This should also serve as a good resource.
Edit: Typo and resources.
如果我没记错的话,DataTemplates 在它们自己的范围内运行,并且不能直接使用
DataTemplate
之外定义的 ElementNames。不过,您可以通过使用StaticResource
并直接从模板内的TextBlock
引用来绕过它。我还没有尝试过 Ragepotatos 的方法来超出 DataTemplate 范围,但很想知道这是否也适合您。
If i remember correctly, DataTemplates run within their own scope and cannot directly use ElementNames defined outside the
DataTemplate
. You could however get around it by usingStaticResource
and referring to that directly fromTextBlock
inside the template.I haven't tried Ragepotatos's approach to go outside DataTemplate scope but would love to know if that works out for you too.