如何在 wpf 中的自定义 window 中查找控件
how to find controls in custom window in wpf
我为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 文件绑定到 "Window.cs" class :
namespace WpfApplication2
{
partial class Window : ResourceDictionary
{
public Windows()
{
InitializeComponent();
}
}
}
但我在 "MainWindow.xaml".
中找不到按钮控件
主要问题:
如何在 wpf
中找到以自定义 window 样式定义的标签控件
有什么想法吗?
请原谅我的英语不好!
试试这个:
this.GetTemplateChild("minimize_window");
您可以了解更多关于此方法的信息here.
Templates in WPF have a self-contained namescope. This is because templates are re-used, and any name defined in a template cannot remain unique when multiple instances of a control each instantiate its template.
您可以使用 FrameworkTemplate.FindName
Method 查找已在 ControlTemplate
中定义的元素,其中 查找与此模板中定义的指定名称关联的元素.
您可以在 MSDN 的 How to: Find ControlTemplate-Generated Elements 页面中看到完整的故事,但这里是从链接页面中摘录的一个简短示例,可以让您了解一下:
// Finding the grid that is generated by the ControlTemplate of the Button
Grid gridInTemplate = (Grid)myButton1.Template.FindName("grid", myButton1);
// Do something to the ControlTemplate-generated grid
MessageBox.Show("The actual width of the grid in the ControlTemplate: "
+ gridInTemplate.GetValue(Grid.ActualWidthProperty).ToString());
您需要在 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;
}
}
我为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 文件绑定到 "Window.cs" class :
namespace WpfApplication2
{
partial class Window : ResourceDictionary
{
public Windows()
{
InitializeComponent();
}
}
}
但我在 "MainWindow.xaml".
中找不到按钮控件
主要问题:
如何在 wpf
有什么想法吗? 请原谅我的英语不好!
试试这个:
this.GetTemplateChild("minimize_window");
您可以了解更多关于此方法的信息here.
Templates in WPF have a self-contained namescope. This is because templates are re-used, and any name defined in a template cannot remain unique when multiple instances of a control each instantiate its template.
您可以使用 FrameworkTemplate.FindName
Method 查找已在 ControlTemplate
中定义的元素,其中 查找与此模板中定义的指定名称关联的元素.
您可以在 MSDN 的 How to: Find ControlTemplate-Generated Elements 页面中看到完整的故事,但这里是从链接页面中摘录的一个简短示例,可以让您了解一下:
// Finding the grid that is generated by the ControlTemplate of the Button
Grid gridInTemplate = (Grid)myButton1.Template.FindName("grid", myButton1);
// Do something to the ControlTemplate-generated grid
MessageBox.Show("The actual width of the grid in the ControlTemplate: "
+ gridInTemplate.GetValue(Grid.ActualWidthProperty).ToString());
您需要在 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;
}
}