gpt4 book ai didi

wpf - 当我在按钮上使用样式或模板时,为什么我的窗口无法加载?

转载 作者:行者123 更新时间:2023-12-04 14:40:21 24 4
gpt4 key购买 nike

我是 WPF 的新手,所以这让我很生气。

考虑我的 xaml 标记中的以下按钮:

<Button Name="btnSMS" Click="btnSMS_Click" Height="30" Width="66"
Margin="10,20,10,10" Background="#FF1E8383" Foreground="White"
Template="{StaticResource RoundedButtonGreen}">Send SMS</Button>

我已经定义了它使用的模板如下(这个想法实际上是为了获得圆角):
<Window.Resources>
<ControlTemplate x:Key="RoundedButtonGreen" TargetType="Button">
<Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Border>
</ControlTemplate>
</Window.Resources>

Window.Resources ,我还有一个我定义的样式,它基本上做同样的事情:
    <!--<Style x:Key="RoundedButtonGreen" TargetType="Button">
<Setter Property="Background" Value="#FF1E8323" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>-->

因此,当我在 Visual Studio 中点击 Debug 时,如果且仅当按钮应用了样式或模板时,我会收到以下错误。

没有样式或模板,窗口加载得很好。
enter image description here

编辑

根据请求,这是整个窗口
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SCADA_Demo"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars" xmlns:dxsch="http://schemas.devexpress.com/winfx/2008/xaml/scheduler" xmlns:dxlc="http://schemas.devexpress.com/winfx/2008/xaml/layoutcontrol" x:Class="SCADA_Demo.MainWindow"
mc:Ignorable="d"
Title="IHS Towers SCADA" WindowStyle="None" Background="#1e1e1e"
Loaded="Window_Loaded">

<Grid>
<DockPanel LastChildFill="True">
<!-- Title Bar -->
<DockPanel LastChildFill="True" DockPanel.Dock="Top" Background="#2d2d30">
<StackPanel DockPanel.Dock="Left" Height="30" Background="#2d2d30" FlowDirection="LeftToRight" Orientation="Horizontal">
<Label Name="lblTitle" HorizontalAlignment="Left" BorderThickness="0" Foreground="White">IHS Towers SCADA</Label>
</StackPanel>
<StackPanel DockPanel.Dock="Right" Height="30" Background="#2d2d30" FlowDirection="RightToLeft" Orientation="Horizontal">
<Button x:Name="btnClose" Content="X" Click="btnClose_Click" Width="30" Foreground="White" Background="#2d2d30" BorderThickness="0"></Button>
<Button Name="btnMinimize" Content="_" Click="btnMinimize_Click" Width="30" Foreground="White" Background="#2d2d30" BorderThickness="0"></Button>
</StackPanel>
</DockPanel>

<!-- Towers Bar across the top -->
<StackPanel DockPanel.Dock="Top" Height="50" Background="#2d2d30" VerticalAlignment="Top" x:Name="spTowers" Orientation="Horizontal">
<Rectangle Name="Tower1" Fill="Green" Height="30" Width="30" Margin="10,5,10,5"></Rectangle>
<Rectangle Name="Tower2" Fill="Red" Height="30" Width="30" Margin="10,5,10,5"></Rectangle>
</StackPanel>

<!-- Alert Window in the center -->
<StackPanel Name="spBody" DockPanel.Dock="Top" Height="396" Background="#b6bcc6"></StackPanel>

<!-- Actions Bar across the bottom -->
<StackPanel DockPanel.Dock="Bottom" Height="60" Background="#2d2d30" VerticalAlignment="Bottom" x:Name="spActions" FlowDirection="RightToLeft" Orientation="Horizontal">

<Button Name="btnSMS" Click="btnSMS_Click" Height="30" Width="66" Margin="10,20,10,10" Background="#FF1E8383" Foreground="White" Template="{StaticResource RoundedButtonGreen}">Send SMS</Button>
</StackPanel>
</DockPanel>
</Grid>

<Window.Resources>
<ControlTemplate x:Key="RoundedButtonGreen" TargetType="Button">
<Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Border>
</ControlTemplate>
<!--<Style x:Key="RoundedButtonGreen" TargetType="Button">
<Setter Property="Background" Value="#FF1E8323" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>-->
</Window.Resources>

</Window>

最佳答案

您应该定义 <Window.Resources>在网格之前。顺序很重要:

<Window ...>
<Window.Resources>
<ControlTemplate x:Key="RoundedButtonGreen" TargetType="Button">
<Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Border>
</ControlTemplate>
<!--<Style x:Key="RoundedButtonGreen" TargetType="Button">
<Setter Property="Background" Value="#FF1E8323" />
<Setter Property="Foreground" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border CornerRadius="4" Background="#FF2AA630" BorderThickness="1">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"></ContentPresenter>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>-->
</Window.Resources>
<Grid>
...
</Window>

关于wpf - 当我在按钮上使用样式或模板时,为什么我的窗口无法加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44157232/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com