M.k*_*ary 1 c# wpf resources button
如何定义全局按钮并在WPF中的多个位置使用它.
这是我的按钮女巫我想在多个地方使用它.
<Button x:Key="Attach" Width="90" Margin="220,0,0,0" Content="Attach" Height="16" FontSize="11"/>
Run Code Online (Sandbox Code Playgroud)
但我试图在App.xaml(Application.Resources)中定义它
以及MainWindow.xaml(内部Window.Resources)
但我无法在CodeBehind中访问它
Button button = Resources["Attach"];
Run Code Online (Sandbox Code Playgroud)
我的问题是在哪里定义我的button,如果我定义它正确如何在CodeBehind和XAML中使用它.
小智 5
在App.xaml中,您必须添加和定义按钮所需的样式.
<Application.Resources>
<Style x:Key="Attach" TargetType="{x:Type Button}">
<Setter Property="Height" Value="16" />
<Setter Property="Width" Value="90" />
<Setter Property="Content" Value="Attach" />
<Setter Property="Margin" Value="220,0,0,0" />
<Setter Property="FontSize" Value="11" />
</Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)
要在代码隐藏中访问它,您需要初始化一个新的样式对象,并使用您在App.xaml中创建的样式填充它.最后,只需将新样式添加到按钮的样式属性中即可.
Style style = this.FindResource("Attach") as Style;
Button.Style = style;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
117 次 |
| 最近记录: |