gpt4 book ai didi

c# - 如何在wpf中的自定义窗口中查找控件

转载 作者:行者123 更新时间:2023-11-30 16:55:43 29 4
gpt4 key购买 nike


我为 Window 设计了一个表单模板,并命名为“WindowStyle1”:

<Style x:Key="WindowStyle1" TargetType="{x:Type Window}">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Border Height="Auto" Width="Auto" Background="#FF27C986" Margin="10">
<Border.Effect>
<DropShadowEffect ShadowDepth="0" Direction="180" Opacity="0.52" BlurRadius="10"/>
</Border.Effect>
<Grid Loaded="OnWindowLoaded">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="94"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="38"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.RowSpan="1" Grid.Row="1" Grid.ColumnSpan="2">
<AdornerDecorator>
<ContentPresenter/>
</AdornerDecorator>
</Border>
<Button Click="minimize_window" x:Name="btn_window_minimize" Grid.Column="1" HorizontalAlignment="Left" Margin="4.565,9.5,0,0" VerticalAlignment="Top" Width="20" Height="19.96" Style="{DynamicResource ButtonStyle_Windows_MinimumSize}" BorderThickness="1"/>
<Button Click="maximize_window" x:Name="btn_window_maximize" Grid.Column="1" HorizontalAlignment="Left" Margin="34.565,9.5,0,0" VerticalAlignment="Top" Width="20" Height="19.96" Style="{DynamicResource ButtonStyle_Windows_MaximumSize}"/>
<Button Click="close_window" x:Name="btn_window_close" Grid.Column="1" HorizontalAlignment="Left" Margin="64,9.5,0,0" VerticalAlignment="Top" Width="20" Height="19.96" Style="{DynamicResource ButtonStyle_Windows_Close}"/>
<Grid MouseLeftButtonDown="Window_MouseLeftButtonDown" HorizontalAlignment="Stretch" Height="38" VerticalAlignment="Top" Background="#FF27C986">
<Label Name="lbl_title" Content="Label" HorizontalAlignment="Left" Margin="0.998,0,0,0" VerticalAlignment="Top" FontSize="20" Foreground="White"/>
</Grid>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="ResizeMode" Value="CanResizeWithGrip">
<Setter Property="Template" Value="{StaticResource WindowTemplateKey}"/>
</Trigger>
</Style.Triggers>
</Style>

然后我将这个 .xaml 文件绑定(bind)到“Window.cs”类:

namespace WpfApplication2
{
partial class Window : ResourceDictionary
{
public Windows()
{
InitializeComponent();
}
}
}

但我在“MainWindow.xaml”中找不到按钮控件。

主要问题:
如何在wpf中找到自定义窗口样式中定义的标签控件

有什么想法吗?请原谅我的英语不好!

最佳答案

您需要在 Loaded 事件(或之后)中执行此操作。

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var label = Template.FindName("lbl_title", this) as Label;
Loaded += MainWindow_Loaded;
}

void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
var label = Template.FindName("lbl_title", this) as Label;
}
}

关于c# - 如何在wpf中的自定义窗口中查找控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29094171/

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