设置用户控件子控件的样式

发布于 2024-12-06 06:40:31 字数 2138 浏览 2 评论 0原文

假设我有一个像这样的用户控件:

<UserControl x:Class="StyleTest.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Button Grid.Row="0">Style please</Button>
    <Button Grid.Row="1">Style please</Button>
</Grid>

我想将此控件中的所有按钮设置为背景=绿色。 但我不想影响程序中的其他按钮,也不想修改控件的代码。

我现在发现的是这样的:

<Window x:Class="StyleTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:loc="clr-namespace:StyleTest"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="UserControlStyles" TargetType="Button">
        <Setter Property="Background" Value="green" />
    </Style>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Button Grid.Column="0">no Style please</Button>
    <loc:UserControl1 Grid.Column="1">
        <loc:UserControl1.Resources>
            <Style TargetType="Button" BasedOn="{StaticResource UserControlStyles}" />
        </loc:UserControl1.Resources>
    </loc:UserControl1>
</Grid>

但这意味着如果我还想设置文本框的前景色等样式,我必须将此代码添加到控件的每个实例中,以及一些额外的代码。

我正在寻找与此类似的东西:

        <Style TargetType="Buttons that are childs of UserControl1">
        <Setter Property="Background" Value="green" />
    </Style>

有办法做到这一点吗?

我认为控件模板还不够,因为我不想重新设计整个控件,我只想设置按钮的颜色。

Say I have a user control like this:

<UserControl x:Class="StyleTest.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Button Grid.Row="0">Style please</Button>
    <Button Grid.Row="1">Style please</Button>
</Grid>

And I want to set all buttons in this control to background=green.
But I don't want to affect other buttons in my program and I don't want to modify the code of the control.

What I found right now is this:

<Window x:Class="StyleTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:loc="clr-namespace:StyleTest"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style x:Key="UserControlStyles" TargetType="Button">
        <Setter Property="Background" Value="green" />
    </Style>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Button Grid.Column="0">no Style please</Button>
    <loc:UserControl1 Grid.Column="1">
        <loc:UserControl1.Resources>
            <Style TargetType="Button" BasedOn="{StaticResource UserControlStyles}" />
        </loc:UserControl1.Resources>
    </loc:UserControl1>
</Grid>

But this would imply that I have to add this code to every instance of the control, and some extra code, if I want to style e.g. foreground color of TextBoxes also.

What I am looking for is something similar to this:

        <Style TargetType="Buttons that are childs of UserControl1">
        <Setter Property="Background" Value="green" />
    </Style>

Is there a way of doing this?

I don't think that a control template would be sufficient, because I don't want to redesign the whole control, I just want to set the color of the buttons.

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

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

发布评论

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

评论(2

鹿港小镇 2024-12-13 06:40:31

在 App.xaml 中,您可以添加此样式,该样式将应用于 UserControl1 的所有实例

<Style TargetType="StyleTest:UserControl1" >
   <Style.Resources>
     <Style TargetType="Button">
        <Setter Property="Background" Value="green" />
     </Style>
   </Style.Resources>
</Style>

In App.xaml you could add this style which will be applied to all instances of UserControl1

<Style TargetType="StyleTest:UserControl1" >
   <Style.Resources>
     <Style TargetType="Button">
        <Setter Property="Background" Value="green" />
     </Style>
   </Style.Resources>
</Style>
我做我的改变 2024-12-13 06:40:31

如果我理解正确,您只需要修改您的 UserControl 以便在其中添加样式,而不是在 Window 控件中

<UserControl x:Name=UserControl1 ...>
<UserControl.Resources>
   <Style TargetType="Button">
        <Setter Property="Background" Value="green" />
   </Style>
</UserControl.Resources>

我刚刚看到您说您不想修改该控件。你的意思是你不能修改UserControl的xaml?为什么不呢?

If I understand correctly, you just need to modify your UserControl so that you add the style there, instead of in the Window control

<UserControl x:Name=UserControl1 ...>
<UserControl.Resources>
   <Style TargetType="Button">
        <Setter Property="Background" Value="green" />
   </Style>
</UserControl.Resources>

I just saw that you said you don't want to modify the control. You mean that you can't modify the xaml of the UserControl? Why not?

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