无法使用 className.property 设置样式

发布于 2024-12-05 17:13:33 字数 1091 浏览 1 评论 0原文

我是 wpf 新手,我想使用样式和 className.property

该样式不适用于第一个文本框

,因为事实上它适用于堆栈面板

我是否遗漏了什么?

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="883">
<Grid>
    <Grid.Resources >
        <Style x:Key="m">
            <Setter Property="TextBox.Background" Value="Aqua"/>
        </Style>
    </Grid.Resources>
    <TextBlock >adsdfsadfasdfad</TextBlock>
    <StackPanel Orientation="Horizontal" Style="{StaticResource m}">
        <TextBox HorizontalAlignment="Stretch" Width="193"  Margin="50">
        </TextBox>
        <TextBox HorizontalAlignment="Stretch" Width="193" Background="Black" Margin="50">
        </TextBox>
    </StackPanel>
</Grid>

请查看这篇文章 在其中找到 Property="ClassName.Property" 谢谢。

Im new to wpf i want to use a style and className.property

the style does not apply to first text box

as the mater of fact it applies to stack panel

Am i missing something?

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="883">
<Grid>
    <Grid.Resources >
        <Style x:Key="m">
            <Setter Property="TextBox.Background" Value="Aqua"/>
        </Style>
    </Grid.Resources>
    <TextBlock >adsdfsadfasdfad</TextBlock>
    <StackPanel Orientation="Horizontal" Style="{StaticResource m}">
        <TextBox HorizontalAlignment="Stretch" Width="193"  Margin="50">
        </TextBox>
        <TextBox HorizontalAlignment="Stretch" Width="193" Background="Black" Margin="50">
        </TextBox>
    </StackPanel>
</Grid>

take a look at this article
find Property="ClassName.Property" in that
thank you.

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

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

发布评论

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

评论(2

有木有妳兜一样 2024-12-12 17:13:33

如果您想为堆栈面板中的所有文本框设置样式,请尝试这个

<Grid>
    <Grid.Resources >
         <Style x:Key="m" TargetType="{x:Type TextBox}">
         <Setter Property="Background" Value="Blue" />
       </Style>
   </Grid.Resources>
 <TextBlock >adsdfsadfasdfad</TextBlock>
      <StackPanel Orientation="Horizontal" >
          <StackPanel.Resources>
               <Style BasedOn="{StaticResource m}" TargetType="{x:Type TextBox}" />
          </StackPanel.Resources>
          <TextBox HorizontalAlignment="Stretch" Name="ss" Width="193" Margin="50"/>
         <TextBox Width="193"  Margin="50"/>
     </StackPanel>
</Grid>

您可以使用 TargetType="{x:Type TextBox} 指定它

If you want to the Set style for all TextBoxes in a stack panel then try this one

<Grid>
    <Grid.Resources >
         <Style x:Key="m" TargetType="{x:Type TextBox}">
         <Setter Property="Background" Value="Blue" />
       </Style>
   </Grid.Resources>
 <TextBlock >adsdfsadfasdfad</TextBlock>
      <StackPanel Orientation="Horizontal" >
          <StackPanel.Resources>
               <Style BasedOn="{StaticResource m}" TargetType="{x:Type TextBox}" />
          </StackPanel.Resources>
          <TextBox HorizontalAlignment="Stretch" Name="ss" Width="193" Margin="50"/>
         <TextBox Width="193"  Margin="50"/>
     </StackPanel>
</Grid>

You can specify it by using the TargetType="{x:Type TextBox}

夜还是长夜 2024-12-12 17:13:33

这里是另一个例子,可以更好地理解 setter 的工作原理:

<Window.Resources>
    <Style x:Key="m">
        <Setter Property="TextBox.Height"
                Value="100" />
    </Style>
</Window.Resources>
<Grid>
    <StackPanel Style="{StaticResource m}">
        <TextBlock>My Sample</TextBlock>
        <TextBox>My text box</TextBox>
    </StackPanel>
</Grid>

这里,TextBox.Height 并不是指 TextBox 的高度,而只是指向一个名为 Height 的依赖属性。此行为是由于该样式没有 TargetType。
因此,在此示例中,文本框的高度将保留为默认值,只有 stackpanel 的高度将更改为 100。

在 FontFamily 的示例中也会发生类似的情况。实际发生的情况是,setter 将 StackPanel FontFamiliy 属性设置为 setter 中的值。另一件需要记住的重要事情是,某些属性是在父控件与其子控件之间继承的。
但您需要小心,因为属性不会被继承。例如 FontFamily 是可以的,但是 Foreground 不能被 TextBox 控件继承。

我认为您可以使用类似内容的唯一方法

Property = "ClassName.Property" 

是当您要应用该样式的控件的可视化树包含 ClassName 类型的元素时。

例如

 <Window.Resources>
    <Style x:Key="m">
        <Setter Property="TextBlock.FontFamily"
                Value="Aharoni" />
    </Style>
 </Window.Resources>
 <Grid>
    <StackPanel Style="{StaticResource m}">
        <TextBlock>My Sample</TextBlock>
    </StackPanel>
 </Grid>

,这里 StackPanel 在其可视化树中包含一个 TextBlock,因此现在将对其应用设置器。

此外,如果您在样式中定义 TargetType,它将知道在哪些控件上应用模板。

此处是一个描述控件可视树的链接。

So here is another example to better understand how the setter works:

<Window.Resources>
    <Style x:Key="m">
        <Setter Property="TextBox.Height"
                Value="100" />
    </Style>
</Window.Resources>
<Grid>
    <StackPanel Style="{StaticResource m}">
        <TextBlock>My Sample</TextBlock>
        <TextBox>My text box</TextBox>
    </StackPanel>
</Grid>

Here, the TextBox.Height is not referring to the height of the TextBox, but is simply pointing to a dependency property called Height. This behavior is due to the fact that the style does not have a TargetType.
So in this sample, the height of the text box will remain as default, and only the height of the stackpanel will change to 100.

Something like that happens also in the example with FontFamily. What actually happens is that the setter sets the StackPanel FontFamiliy property to the value in the setter. Another important thing to keep in mind is that some properties are inherited between the parent control and their children.
But you need to be careful as not properties are inherited. For example FontFamily is ok, but Foreground is not inherited by TextBox control.

I think the the only way you can use something like

Property = "ClassName.Property" 

is when the visual tree of the control on which you want to apply that style contains an element of type ClassName.

For example

 <Window.Resources>
    <Style x:Key="m">
        <Setter Property="TextBlock.FontFamily"
                Value="Aharoni" />
    </Style>
 </Window.Resources>
 <Grid>
    <StackPanel Style="{StaticResource m}">
        <TextBlock>My Sample</TextBlock>
    </StackPanel>
 </Grid>

Here the StackPanel contains in it's visual tree a TextBlock, and consequently is will now to apply the setter to it.

Further more, if you define a TargetType inside the style, it will know on which controls to apply the template.

Here is a link that describes a little what is the visual tree of a control.

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