将名称添加到特定样式控件 wpf
Add name to specific style control wpf
我有一个自定义上下文菜单:
<Window.Resources>
<ContextMenu x:Key="RowMenu" DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
<ContextMenu.Style>
<Style TargetType="ContextMenu">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Background="Transparent">
<Border Background="#1c1c1c" Height="70" Width="170" CornerRadius="10">
<StackPanel Orientation="Vertical">
<Button x:Name="openinBrowser" Click="Button_Click_1">
<Grid Width="170">
<materialDesign:PackIcon Kind="OpenInApp" VerticalAlignment="Center" Foreground="{StaticResource PrimaryHueMidBrush}" HorizontalAlignment="Left"/>
<Label FontFamily="Champagne & Limousines" Content="Action 1" FontSize="7" HorizontalAlignment="Center" Foreground="LightGray" VerticalAlignment="Center"/>
</Grid>
<Button.Style>
<Style BasedOn="{StaticResource MaterialDesignRaisedAccentButton}" TargetType="Button">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="{StaticResource PrimaryHueMidBrush}"/>
<Setter Property="BorderThickness" Value="0"/>
</Style>
</Button.Style>
</Button>
</StackPanel>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ContextMenu.Style>
</ContextMenu>
</Window.Resources>
我怎样才能给按钮添加一个名字,这样我就可以在我的 c# 中启用和禁用它(不使用绑定),我试过输入 x:Name=""
但它不起作用,但是如果我添加一个按钮单击它有效吗?我很困惑,任何帮助将不胜感激!
我仍然说你应该通过数据绑定正确地做到这一点,但如果你坚持......有几种不同的方法可以解决这个问题。
上下文菜单不是常规可视化树的一部分,因此您必须直接访问它们。为您的上下文菜单命名,然后通过遍历其模板的可视化树找到按钮:
// button has to be templated in order for this to work,
// so don't try it in the parent window's constructor
// (add a this.contextMenu.Loaded handler instead if you have to)
var button = this.contextMenu.Template.FindName("openinBrowser", this.contextMenu) as Button;
如果您的可视化树特别复杂,那么更快的选择是在 window 的资源块中创建布尔资源:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
<Window.Resources>
<sys:Boolean x:Key="ButtonEnabled">True</sys:Boolean>
</Window.Resources>
...然后将您的按钮动态绑定到该按钮:
<Button x:Name="openinBrowser" IsEnabled="{DynamicResource ButtonEnabled}">
虽然这违反了您的 "no binding" 规则,这就是为什么我问您为什么如此坚持不使用数据绑定...您仍然可以使用它,即使您没有绑定到数据上下文。在这种情况下,您改为在代码中设置该资源的值:
this.Resources["ButtonEnabled"] = false;
我有一个自定义上下文菜单:
<Window.Resources>
<ContextMenu x:Key="RowMenu" DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
<ContextMenu.Style>
<Style TargetType="ContextMenu">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Grid Background="Transparent">
<Border Background="#1c1c1c" Height="70" Width="170" CornerRadius="10">
<StackPanel Orientation="Vertical">
<Button x:Name="openinBrowser" Click="Button_Click_1">
<Grid Width="170">
<materialDesign:PackIcon Kind="OpenInApp" VerticalAlignment="Center" Foreground="{StaticResource PrimaryHueMidBrush}" HorizontalAlignment="Left"/>
<Label FontFamily="Champagne & Limousines" Content="Action 1" FontSize="7" HorizontalAlignment="Center" Foreground="LightGray" VerticalAlignment="Center"/>
</Grid>
<Button.Style>
<Style BasedOn="{StaticResource MaterialDesignRaisedAccentButton}" TargetType="Button">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="{StaticResource PrimaryHueMidBrush}"/>
<Setter Property="BorderThickness" Value="0"/>
</Style>
</Button.Style>
</Button>
</StackPanel>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ContextMenu.Style>
</ContextMenu>
</Window.Resources>
我怎样才能给按钮添加一个名字,这样我就可以在我的 c# 中启用和禁用它(不使用绑定),我试过输入 x:Name=""
但它不起作用,但是如果我添加一个按钮单击它有效吗?我很困惑,任何帮助将不胜感激!
我仍然说你应该通过数据绑定正确地做到这一点,但如果你坚持......有几种不同的方法可以解决这个问题。
上下文菜单不是常规可视化树的一部分,因此您必须直接访问它们。为您的上下文菜单命名,然后通过遍历其模板的可视化树找到按钮:
// button has to be templated in order for this to work,
// so don't try it in the parent window's constructor
// (add a this.contextMenu.Loaded handler instead if you have to)
var button = this.contextMenu.Template.FindName("openinBrowser", this.contextMenu) as Button;
如果您的可视化树特别复杂,那么更快的选择是在 window 的资源块中创建布尔资源:
xmlns:sys="clr-namespace:System;assembly=mscorlib"
<Window.Resources>
<sys:Boolean x:Key="ButtonEnabled">True</sys:Boolean>
</Window.Resources>
...然后将您的按钮动态绑定到该按钮:
<Button x:Name="openinBrowser" IsEnabled="{DynamicResource ButtonEnabled}">
虽然这违反了您的 "no binding" 规则,这就是为什么我问您为什么如此坚持不使用数据绑定...您仍然可以使用它,即使您没有绑定到数据上下文。在这种情况下,您改为在代码中设置该资源的值:
this.Resources["ButtonEnabled"] = false;