作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我为 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/
我是一名优秀的程序员,十分优秀!