内容属性不止一次

本文关键字:不止一次 属性 | 更新日期: 2023-09-27 18:27:53

以下是显示错误的代码,即:内容属性不止一次代码:

<Window x:Class="Trigger.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">
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
    <Style.Triggers>
        <Trigger Property="IsPressed" Value="True">
            <Setter Property="Opacity" Value="0.5" />
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="Red" />
        </Trigger>
    </Style.Triggers>
</Style>
<Grid>
    <Button x:Name="PropertyTriggerButton" Width="160" Height="40" Margin="20,0,0,0"    HorizontalAlignment="Left"  Content="IsPressed Property"
          Cursor="Hand"  FontWeight="Bold"    Style="{StaticResource ButtonStyle}"    ToolTip="Press To Raise Property Trigger">            
    </Button>
    </Grid>
</Window>

内容属性不止一次

Window元素只能承载一个子元素。您需要将Style放入资源中。这样的东西就可以了:

<Window.Resources>
    <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
        <Style.Triggers>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Opacity" Value="0.5" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="Red" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

将其放在Window元素的起始标记之后或Window元素的结束标记之前。

完整的代码应该是这样的:

<Window x:Class="Trigger.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>
        <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
            <Style.Triggers>
                <Trigger Property="IsPressed" Value="True">
                    <Setter Property="Opacity" Value="0.5" />
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <Button x:Name="PropertyTriggerButton" Width="160" Height="40" Margin="20,0,0,0"    HorizontalAlignment="Left"  Content="IsPressed Property"
              Cursor="Hand"  FontWeight="Bold"    Style="{StaticResource ButtonStyle}"    ToolTip="Press To Raise Property Trigger">            
        </Button>
    </Grid>
</Window>

在Window.Resources 中添加样式

<Window.Resources>
    <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
        <Style.Triggers>
            <Trigger Property="IsPressed" Value="True">
                <Setter Property="Opacity" Value="0.5" />
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Foreground" Value="Red" />
            </Trigger>
        </Style.Triggers>
    </Style>
    </Window.Resources>
    <Grid >
        <Button x:Name="PropertyTriggerButton" Width="160" Height="40" Margin="20,0,0,0"    HorizontalAlignment="Left"  Content="IsPressed Property"
          Cursor="Hand"  FontWeight="Bold"    Style="{StaticResource ButtonStyle}"    ToolTip="Press To Raise Property Trigger">
        </Button>
    </Grid>