使用 XAML 为 UserControl 中的所有元素添加前景色设置器

发布于 2024-10-09 17:38:41 字数 884 浏览 4 评论 0原文

我的用户控件包含许多标签。在 XAML 中,我想定义一个设置器,允许客户端同时设置所有这些的前景。


源代码:(简化)

在Page.Resources下:

<DataTemplate x:Key="customItemTemplate">
    <StackPanel Orientation="Horizontal">
        <MyControlLib:XYControl Unit="{Binding XYUnit}"/>
            <TextBlock Text="{Binding XYMultiplier}" Width="16"/>
    </StackPanel>
</DataTemplate>

在页面内容中:(

<ListBox x:Name="XYZList" ItemTemplate="{StaticResource customItemTemplate}">
    <!-- Set Foreground to "Red" for all items -->
    <!-- For XYControl this is the TextForeground property -->
    <!-- For TextBlock this is (naturally) the Foreground property -->
</ListBox>

阅读XAML注释以了解我想要实现的WPF伟大之处)

当然,customItemTemplate用于页面中不止一处,具有不同的颜色。

在 WPF 中这该多简单啊!

My UserControl contains a number of Labels. In XAML I want to define a setter that allows clients to set the Foreground for all of these at once.


Source code: (simplified)

Under Page.Resources:

<DataTemplate x:Key="customItemTemplate">
    <StackPanel Orientation="Horizontal">
        <MyControlLib:XYControl Unit="{Binding XYUnit}"/>
            <TextBlock Text="{Binding XYMultiplier}" Width="16"/>
    </StackPanel>
</DataTemplate>

In Page contents:

<ListBox x:Name="XYZList" ItemTemplate="{StaticResource customItemTemplate}">
    <!-- Set Foreground to "Red" for all items -->
    <!-- For XYControl this is the TextForeground property -->
    <!-- For TextBlock this is (naturally) the Foreground property -->
</ListBox>

(Read the XAML comments for the WPF greatness I want to achieve)

Of course, customItemTemplate is used in more than one place in the page, with a different color.

How simple could it be in WPF!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二货你真萌 2024-10-16 17:38:41

如果您希望 UserControl 的用户能够在外部设置该值,则可以定义一个新的 DependencyProperty,然后可以在控件的任何实例上设置该值。

public static readonly DependencyProperty LabelForegroundProperty = DependencyProperty.Register(
    "LabelForeground",
    typeof(Brush),
    typeof(MyUserControl),
    new UIPropertyMetadata(Brushes.Black));

public Brush LabelForeground
{
    get { return (Brush)GetValue(LabelForegroundProperty); }
    set { SetValue(LabelForegroundProperty, value); }
}

然后,您可以在绑定到该值的 UserControl 内为 Label 创建默认样式:

<UserControl.Resources>
    <Style TargetType="{x:Type Label}">
        <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MyUserControl}}, Path=LabelForeground}" />
    </Style>
</UserControl.Resources>

然后,该控件的任何实例都可以设置自己的值,该值将应用于其自己的标签:

<MyUserControl LabelForeground="Red"/>

If you want the value to be able to be set externally by users of the UserControl, you can define a new DependencyProperty, which can then be set on any instance of the control.

public static readonly DependencyProperty LabelForegroundProperty = DependencyProperty.Register(
    "LabelForeground",
    typeof(Brush),
    typeof(MyUserControl),
    new UIPropertyMetadata(Brushes.Black));

public Brush LabelForeground
{
    get { return (Brush)GetValue(LabelForegroundProperty); }
    set { SetValue(LabelForegroundProperty, value); }
}

You can then create a default Style for Label inside the UserControl that binds to this value:

<UserControl.Resources>
    <Style TargetType="{x:Type Label}">
        <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MyUserControl}}, Path=LabelForeground}" />
    </Style>
</UserControl.Resources>

Any instance of the control can then set its own value that will be applied to its own Labels:

<MyUserControl LabelForeground="Red"/>
沫尐诺 2024-10-16 17:38:41

我相信这个例子会对你有所帮助。
样式在父节点中定义,因此它对所有标签生效,并且在其后面的代码中被新样式替换。

XAML:

 <StackPanel x:Name="root">
    <StackPanel.Resources>
        <Style  TargetType="Label">
            <Setter Property="Foreground" Value="Red"/>
        </Style>
    </StackPanel.Resources>
    <Label>AAA</Label>
    <Label>BBB</Label>
    <Button Click="Button_Click">Change Color</Button>
</StackPanel>

隐藏代码:

  private void Button_Click(object sender, RoutedEventArgs e)
    {
        var x = new Style();
        x.Setters.Add(new Setter { Property = TextBlock.ForegroundProperty, Value = new SolidColorBrush(Colors.Green) });

        root.Resources.Clear();
        root.Resources.Add(typeof(Label), x);
    }

I believe this example will help you.
A style is defined in a parent node so it takes effect on all the labels, and in the code behind it's beeing replaced by a new style.

XAML:

 <StackPanel x:Name="root">
    <StackPanel.Resources>
        <Style  TargetType="Label">
            <Setter Property="Foreground" Value="Red"/>
        </Style>
    </StackPanel.Resources>
    <Label>AAA</Label>
    <Label>BBB</Label>
    <Button Click="Button_Click">Change Color</Button>
</StackPanel>

Code behind:

  private void Button_Click(object sender, RoutedEventArgs e)
    {
        var x = new Style();
        x.Setters.Add(new Setter { Property = TextBlock.ForegroundProperty, Value = new SolidColorBrush(Colors.Green) });

        root.Resources.Clear();
        root.Resources.Add(typeof(Label), x);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文