我怎样才能使这个装饰标签控件工作?
How can I make this decorated Label control work?
我觉得这应该是一件简单的事情,但显然我在做一些愚蠢的事情。
我正在尝试制作一个带有 DependencyProperty
(枚举)的 UserControl
来控制它是否简单地逐字显示其 Content
属性 中提供的文本,或附加一些额外的文字。
用户控件:
<UserControl x:Class="Controls.UserControls.IndicatorLabel"
x:Name="indicatorLabel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:enum="clr-namespace:MyNamespace;assembly=EnumAssembly">
<Label>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Content, ElementName=indicatorLabel}" />
<TextBlock FontStyle="Italic"
Margin="5,0,0,0">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility"
Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding IndicatorType}"
Value="{x:Static enum:LabelIndicator.Optional}">
<Setter Property="Text"
Value="(Optional)" />
<Setter Property="Visibility"
Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding IndicatorType}"
Value="{x:Static enum:LabelIndicator.Required}">
<Setter Property="Text"
Value="(Required)" />
<Setter Property="Visibility"
Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
</Label>
</UserControl>
UserControl 的代码隐藏:
public partial class IndicatorLabel : UserControl
{
public static DependencyProperty IndicatorTypeProperty = DependencyProperty.Register("IndicatorType", typeof(LabelIndicator), typeof(IndicatorLabel));
public LabelIndicator IndicatorType
{
get => (LabelIndicator)GetValue(IndicatorTypeProperty);
set => SetValue(IndicatorTypeProperty, value);
}
public IndicatorLabel()
{
InitializeComponent();
}
}
在另一个 XAML 页面中使用 UserControl:
<uc:IndicatorLabel Grid.Row="4"
Content="Description"
IndicatorType="Optional" />
我希望看到:描述 (可选)
事实上,只要设置了 Content
属性,我就无法让 UserControl 的 XAML 中的内容产生任何影响。
编辑:
之所以说它没有效果是因为即使在第一个TextBlock
的Text
属性中放置一个文字字符串,[=15=中的任何内容] 属性 仍然显示。似乎 TextBlock
都没有真正显示出来。
那么为什么会这样呢?是否无法使用 Content
属性 作为将文本放入我真正要显示的部分的方法?我在这里违反了一些关于 Content
的 WPF 规则吗?
首先;是的,您不想使用 Content
。你的 XAML 对于你的 UC 真的 说的是:
<UserControl>
<UserControl.Content>
... All that stuff
</UserControl.Content>
</UserControl>
因此,当您在父 XAML 中分配内容时,您只是覆盖了所有内容。请改用其他 属性(如 "Text")。如果不是 UserControl
你做了一个实际的 Control
(有单独的 ControlTemplate
和所有东西)那么你可以使用那个名字
此外,{Binding IndicatorType}
正在查看继承的数据上下文。您应该将用户控件的数据上下文设置为其自身,或者使用 x:Reference
告诉它查看用户控件本身。
我觉得这应该是一件简单的事情,但显然我在做一些愚蠢的事情。
我正在尝试制作一个带有 DependencyProperty
(枚举)的 UserControl
来控制它是否简单地逐字显示其 Content
属性 中提供的文本,或附加一些额外的文字。
用户控件:
<UserControl x:Class="Controls.UserControls.IndicatorLabel"
x:Name="indicatorLabel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:enum="clr-namespace:MyNamespace;assembly=EnumAssembly">
<Label>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Content, ElementName=indicatorLabel}" />
<TextBlock FontStyle="Italic"
Margin="5,0,0,0">
<TextBlock.Style>
<Style TargetType="TextBlock">
<Setter Property="Visibility"
Value="Collapsed" />
<Style.Triggers>
<DataTrigger Binding="{Binding IndicatorType}"
Value="{x:Static enum:LabelIndicator.Optional}">
<Setter Property="Text"
Value="(Optional)" />
<Setter Property="Visibility"
Value="Visible" />
</DataTrigger>
<DataTrigger Binding="{Binding IndicatorType}"
Value="{x:Static enum:LabelIndicator.Required}">
<Setter Property="Text"
Value="(Required)" />
<Setter Property="Visibility"
Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBlock.Style>
</TextBlock>
</StackPanel>
</Label>
</UserControl>
UserControl 的代码隐藏:
public partial class IndicatorLabel : UserControl
{
public static DependencyProperty IndicatorTypeProperty = DependencyProperty.Register("IndicatorType", typeof(LabelIndicator), typeof(IndicatorLabel));
public LabelIndicator IndicatorType
{
get => (LabelIndicator)GetValue(IndicatorTypeProperty);
set => SetValue(IndicatorTypeProperty, value);
}
public IndicatorLabel()
{
InitializeComponent();
}
}
在另一个 XAML 页面中使用 UserControl:
<uc:IndicatorLabel Grid.Row="4"
Content="Description"
IndicatorType="Optional" />
我希望看到:描述 (可选)
事实上,只要设置了 Content
属性,我就无法让 UserControl 的 XAML 中的内容产生任何影响。
编辑:
之所以说它没有效果是因为即使在第一个TextBlock
的Text
属性中放置一个文字字符串,[=15=中的任何内容] 属性 仍然显示。似乎 TextBlock
都没有真正显示出来。
那么为什么会这样呢?是否无法使用 Content
属性 作为将文本放入我真正要显示的部分的方法?我在这里违反了一些关于 Content
的 WPF 规则吗?
首先;是的,您不想使用 Content
。你的 XAML 对于你的 UC 真的 说的是:
<UserControl>
<UserControl.Content>
... All that stuff
</UserControl.Content>
</UserControl>
因此,当您在父 XAML 中分配内容时,您只是覆盖了所有内容。请改用其他 属性(如 "Text")。如果不是 UserControl
你做了一个实际的 Control
(有单独的 ControlTemplate
和所有东西)那么你可以使用那个名字
此外,{Binding IndicatorType}
正在查看继承的数据上下文。您应该将用户控件的数据上下文设置为其自身,或者使用 x:Reference
告诉它查看用户控件本身。