- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个绑定(bind)到对象的 ItemsControl,在 ItemsControl 的数据模板中,我有两个文本 block ,我想将第一个文本 block 的文本属性绑定(bind)到位于此 ItemsControl 之外的另一个文本 block 。
我已经尝试在父数据上下文中找到对象,并且只是尝试使用 Path=Text 找到 TextBlock
一个例子如下:
<TextBlock Name="Name" Text="{Binding Name}"
Grid.Column="0"
FontSize="{DynamicResource SmallSize}"
TextWrapping="Wrap"
TextAlignment="Right"
Padding="4,0,0,0"
Grid.ColumnSpan="2" Background="Aqua"/>
<ItemsControl ItemsSource="{Binding TheValue}"
Padding="4,0,0,0"
Grid.Column="2"
HorizontalAlignment="Right">
<ItemsControl.ItemTemplate>
<DataTemplate>
<WrapPanel>
<TextBlock Text = "{
Binding RelativeSource =
{RelativeSource FindAncestor,
AncestorType={x:Type Window}}, Path=Name}"
Grid.Column="0"
FontSize="{DynamicResource SmallSize}"
TextWrapping="Wrap" ........................
最佳答案
{Binding RelativeSource = {RelativeSource FindAncestor,
AncestorType={x:Type Window}}, Path=Name}
<TextBlock Text = "{Binding ElementName=Name, Path=Text}"/>
class MainViewModel
{
public Class1 C1 { get; set; }
public Class2 C2 { get; set; }
public MainViewModel()
{
C1 = new Class1 { S1 = "This is C1 data context" };
C2 = new Class2 { S2 = "This is C2 data context" };
}
}
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type local:MainViewModel}">
<StackPanel>
<ContentControl Name="cc1" Content="{Binding C1}"/>
<ContentControl Name="cc2" Content="{Binding C2}"/>
</StackPanel>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Class1}">
<TextBlock Text="{Binding S1}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Class2}">
<TextBlock Text="{Binding ElementName=cc1, Path=DataContext.C1.S1}"/>
</DataTemplate>
</Window.Resources>
<Grid>
<ContentControl Content="{Binding}"/>
</Grid>
</Window>
关于wpf - 在 Itemscontrol 数据模板中使用 FindAncestor 在数据模板之外查找文本 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13496495/
我正在使用 M-V-VM 并在我的 ViewModel 上有一个名为“EntitySelectedCommand”的命令。 我试图让 ItemsControl 中的所有项目来触发这个命令,但是它不起作
在我的 View 中有一个 ItemsControl,它绑定(bind)到来自 ViewModel 的 ObservableCollection。集合被填充,然后引发从 VM 到 View 的事件(想
我最近遇到了一个虚拟化问题,我将其缩小到以下代码。 虚拟化在以下代码段中不起作用的原因是因为 child 没有特定的高度。所以我的猜测是它会永远扩展并且虚拟化会中断。 给 child 一个特定的高度解
我是 WPF 的新手。我一直在尝试执行以下操作: 以下是我的数据结构: public class TokenItems {public string items {get;set;} public i
我正在尝试从 WPF MVVM 教程中扩展应用程序作为练习。对于我在这里面临的这个特定问题,我在网上没有找到解决方案。 我有一个名为“StudentsToAdd”的带有 ObservableColle
我正在使用带有 MVVM 模式的 Wpf,所以在 Xamel 中,我有一个 itemControl 持有另一个 itemcontrol,每个 itemcontrol 绑定(bind)来自不同的 Obs
标题可能听起来令人费解,但请耐心等待。 我有包含住户的房间: public class Room { public string Name { get; set; } public L
我需要能够从子 ItemsControll 数据模板内部绑定(bind)到父 ItemsControl 的属性:
这让我发疯。也许我的数据设计有误,但我正在尝试从父 ItemsControl 绑定(bind)到事件项目。 每个区域都有一种颜色,以便在屏幕上轻松识别。我没有为每个座位赋予颜色属性,而是在 Area
我的问题与此非常相似:( Binding events to buttons in an ItemsControl ) 但我没有在那里找到解决方案。我有一个 ItemsControl,在它的 Data
考虑以下 XAML
在我的主视图中,我有一个绑定(bind)到对象集合的 ItemsControl: ActivationLevelTemplate 只是另一个 View : 在这个 View 中有一
我正在使用 ItemsControl,其中 ItemsPanel 设置为 Canvas(有关更多背景信息,请参阅 this 问题)。ItemsControl 正在按我想要的方式执行,并且通过将子元素放
我有一个 ScrollViewer,里面有一个 ItemsControl。 ItemsControl 的 ItemSource 绑定(bind)到 ObservableCollection。 问题是它
我有一个带有 itemscontrol 的 WPF MVVM 应用程序。水平子项的数量受 itemscontrol 宽度的影响。只有一件事我没有解决。我试图以始终居中的方式对齐子元素。我已经用油漆制作
我有以下 XAML: 当我将数据拖到此面板上时,鼠标光标显示允许在所有子项上放置,但在任何空白区域上,光标显示禁止放置。如果我设置 AllowDro
我有一些数据要显示在 FlowDocument 中.这基本上是一个以友好方式解释数据的 View ,包括节标题、文本段落等,我将在 FlowDocumentScrollViewer 中显示这些 Vie
我有一个 ItemsControl包含我想虚拟化的数据列表,但是 VirtualizingStackPanel.IsVirtualizing="True"似乎不适用于 ItemsControl . 真
我想写一个 ItemsControl 派生的自定义控件。这部分是出于需要,部分是作为学习练习 - 请不要建议 I Style、DataTemplate、ControlTemplate 和 ListBo
我想使用ItemsControl显示重要的项目列表。 我使用ItemsControl的原因是,我正在处理的应用程序中的DataTemplate要复杂得多:提供的示例代码仅反射(reflect)了我的大
我是一名优秀的程序员,十分优秀!