C# Can';t访问ControlTemplate中TextBlock的Text属性
我试图将属性数据绑定到标签,并根据绑定属性的值更改其颜色和文本。我使用ControlTemplate更改颜色和文本,因为更改标签内容以响应DataTriggers不起作用(文本从未出现) 因此,在标签上内联定义ControlTemplate时,使用ControlTemplate是有效的,但在将模板定义为资源时,似乎不起作用 下面的代码是一个更简单的例子来演示这个问题 这就是我到目前为止所做的:C# Can';t访问ControlTemplate中TextBlock的Text属性,c#,wpf,C#,Wpf,我试图将属性数据绑定到标签,并根据绑定属性的值更改其颜色和文本。我使用ControlTemplate更改颜色和文本,因为更改标签内容以响应DataTriggers不起作用(文本从未出现) 因此,在标签上内联定义ControlTemplate时,使用ControlTemplate是有效的,但在将模板定义为资源时,似乎不起作用 下面的代码是一个更简单的例子来演示这个问题 这就是我到目前为止所做的: <ResourceDictionary> <ControlT
<ResourceDictionary>
<ControlTemplate x:Key="baseTemplate" TargetType="{x:Type Label}">
<Grid Background="{TemplateBinding Background}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="24"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="24"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="InnerTextBlock" Grid.Column="1"
Text="{TemplateBinding Label.Content}" <!-- An attempt to tie the Text here to the Label's Content property -->
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
Foreground="{TemplateBinding Foreground}"
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"
/>
</Grid>
</ControlTemplate>
<Style x:Key="availableLabelStyle" TargetType="{x:Type Label}">
<Setter Property="Background" Value="#FF567E4A"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Content" Value="Available"/>
<Setter Property="Template" Value="{StaticResource baseTemplate}"/>
</Style>
</ResourceDictionary>
<Label x:Name="StatusLabel"
Style="{StaticResource availableLabelStyle}"
Grid.Column="1"
HorizontalAlignment="Left"
Margin="111,71,0,0"
VerticalAlignment="Top" Width="124"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Height="18"
Padding="2"
/>
VerticalAlignment=“{TemplateBinding VerticalContentAlignment}”
HorizontalAlignment=“{TemplateBinding HorizontalContentAlignment}”
Padding=“{TemplateBinding Padding}”
Background=“{TemplateBinding Background}”
前台=“{TemplateBinding前台}”
Width=“{TemplateBinding Width}”
Height=“{TemplateBinding Height}”
/
问题是“availableLabelStyle”的Setter中的Content属性似乎不起作用。将此样式应用于标签时,不会显示任何文本
有没有更好的方法在这里执行相同的操作并使文本显示在标签中
提前感谢您在这方面的帮助。您的代码正在运行。以下是我的完整示例:
<Window x:Class="WPFTestApp2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ResourceDictionary>
<ControlTemplate x:Key="baseTemplate" TargetType="{x:Type Label}">
<Grid Background="{TemplateBinding Background}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="24"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="24"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="1" Text="{TemplateBinding Label.Content}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
Padding="{TemplateBinding Padding}"
Background="{TemplateBinding Background}"
Foreground="{TemplateBinding Foreground}"
Width="{TemplateBinding Width}"
Height="{TemplateBinding Height}"/>
</Grid>
</ControlTemplate>
<Style x:Key="availableLabelStyle" TargetType="{x:Type Label}">
<Setter Property="Background" Value="#FF567E4A"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Content" Value="Available"/>
<Setter Property="Template" Value="{StaticResource baseTemplate}"/>
</Style>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Label x:Name="StatusLabel" Style="{StaticResource availableLabelStyle}"
Grid.Column="1"
HorizontalAlignment="Left"
Margin="111,71,0,0"
VerticalAlignment="Top" Width="124"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Height="18"
Padding="2"/>
</Grid>
</Window>
将生成以下输出:
另一种方法是使用
ContentPresenter
而不是TextBlock
。您仍然可以向其中添加所有附加属性(至少是您已经显示的属性),并且它将允许显示文本以外的内容。我刚刚在一个空白的混合项目中尝试了这一点(将ControlTemplate/Style复制到Window.Resources中,并将标签添加到网格中),效果很好。标签显示为绿色。白色文本。标签上有“可用”。@Tim谢谢。我正在开发的窗口上一定有别的东西挡住了。在这里提出这个问题之前,我应该在一个测试项目中测试这个。谢谢你帮我研究这个问题。我得检查一下为什么我的窗户不能用。我有一个网格,上面有一堆标签,它们的工作原理都很相似。没有一个显示文本。奇怪,我发现了问题。我继承了我正在开发的窗口,我没有注意到每个标签的DataContext都被设置为代码隐藏中的属性。这妨碍了我在XAML中的新绑定。诅咒!