如这段代码所示,我有一个嵌套在两个堆栈面板内的按钮。
<StackPanel Grid.Row="1" DataContext="{StaticResource AutomaticUISettings}">
<StackPanel Orientation="Horizontal">
<!--Header-->
<Label Content="If Max UI value is met" />
<!--Max UI Value Met Help-->
<Button Content="[?]" Command="{Binding ShowMaxValueMetHelp}" Margin="5" Foreground="DarkCyan" FontSize="10">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
<!--Options-->
<RadioButton GroupName="MeetMaxUI" IsChecked="{Binding Path=Default.MaxMetRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Stop}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Stop" Margin="10,0" />
<RadioButton GroupName="MeetMaxUI" IsChecked="{Binding Path=Default.MaxMetRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=RollOver}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Roll over to zero" Margin="10,0" />
<RadioButton GroupName="MeetMaxUI" IsChecked="{Binding Path=Default.MaxMetRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=RollOvertoMin}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Roll over to Min UI value" Margin="10,0" />
</StackPanel>
整件事都在 a Grid
in a GroupBox
of a 中UserControl
。单击“Max UI Value Met Help”按钮不起作用。我在Grid
上面有类似的帮助按钮,StackPanel
效果很好。我试图通过这个复制并粘贴它们,但它们也不起作用。
为什么这个按钮不起作用?
更新: 我必须切换到网格并将标签和按钮与单选按钮堆栈分开才能使其工作,但我想了解为什么这不起作用。
这里的按钮与我的 UserControl 的其余部分在同一个 DataContext 中。我在整个 UserControl 中有多个帮助按钮,它们可以工作,但这个没有。我放哪一个也没关系。我可以将特定按钮移动到另一个位置并且它可以工作。
我不喜欢在不理解为什么必须这样做的情况下修复某些东西。
新代码:
<!--If Max UI is Met-->
<StackPanel Orientation="Horizontal" Grid.Row="1">
<!--Header-->
<Label Content="If Max UI value is met" />
<!--Max UI Value Met Help-->
<Button Content="[?]" Command="{Binding ShowMaxValueMetHelp}" Margin="5" Foreground="DarkCyan" FontSize="10">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
<!--Options-->
<StackPanel Grid.Row="2" DataContext="{StaticResource AutomaticUISettings}">
<RadioButton GroupName="MeetMaxUI" IsChecked="{Binding Path=Default.MaxMetRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Stop}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Stop" Margin="10,0" />
<RadioButton GroupName="MeetMaxUI" IsChecked="{Binding Path=Default.MaxMetRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=RollOver}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Roll over to zero" Margin="10,0" />
<RadioButton GroupName="MeetMaxUI" IsChecked="{Binding Path=Default.MaxMetRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=RollOvertoMin}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Roll over to Min UI value" Margin="10,0" />
</StackPanel>
这是我去 Grids 之前的代码:
<UserControl x:Class="BogusProgram.DummyUI"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:autoUIsettings="clr-namespace:BogusProgram.Resources"
xmlns:converters ="clr-namespace:BogusProgram.Converters"
xmlns:views="clr-namespace:BogusProgram.Views"
mc:Ignorable="d" >
<!--UserControl Resources-->
<UserControl.Resources>
<!--Automatic UI Settings-->
<autoUIsettings:AutoUISettings x:Key="AutomaticUISettings" />
<!--Enum to Boolean Converter-->
<converters:EnumBooleanConverter x:Key="enumBooleanConverter" />
</UserControl.Resources>
<!--UserControl Command Bindings-->
<UserControl.CommandBindings>
<!--Compare Change of all settings to Last set-->
<CommandBinding Command="{x:Static views:SetupAutoUI.CompareSettings}" Executed="CommandBinding_Executed"/>
<!--Note: Can use individual RoutedCommands per object-->
<!--http://stackoverflow.com/questions/254992/how-can-i-best-handle-wpf-radio-buttons-->
</UserControl.CommandBindings>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!--Enable/Disable Automatic UI-->
<CheckBox Content="Enable Automatic UI" IsChecked="{Binding Source={x:Static autoUIsettings:AutoUISettings.Default}, Path=AutoUIEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" x:Name="AutoUICheckbox" Margin="10,5" />
<!--Automatic UI Settings-->
<GroupBox Header="Automatic UI Settings" Grid.Row="1" Margin="10,5"
IsEnabled="{Binding Source={x:Static autoUIsettings:AutoUISettings.Default}, Path=AutoUIEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<!--Set Automatic UI change by value-->
<StackPanel Orientation="Horizontal" Margin="10,5">
<Label Content="Change UI By: " />
<TextBox Text="{Binding Source={x:Static autoUIsettings:AutoUISettings.Default}, Path=ChangUIby, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="35" x:Name="CHuiBy"
TextChanged="CHuiBy_TextChanged" KeyDown="TextBox_KeyDown" LostFocus="TextBox_LostFocus" />
</StackPanel>
<!--Set when to change UI-->
<StackPanel Orientation="Horizontal" Grid.Row="1" Margin="10,5">
<Label Content="Change UI Every:" />
<TextBox Text="{Binding Source={x:Static autoUIsettings:AutoUISettings.Default}, Path=WaitPeriod, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="35" x:Name="CHuiEv"
TextChanged="CHuiEv_TextChanged" KeyDown="TextBox_KeyDown" LostFocus="TextBox_LostFocus" />
<!--Change UI Every Help-->
<Button Content="[?]" Command="{Binding ShowCHuiEvHelp}" Margin="5" Foreground="DarkCyan" FontSize="10">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
<!--Change UI for-->
<StackPanel Orientation="Horizontal" Grid.Row="2" Margin="10,5" IsEnabled="{Binding Source={x:Static autoUIsettings:AutoUISettings.Default}, Path=RunPeriodEnabled, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}">
<Label Content="Change UI For: " />
<TextBox Text="{Binding Source={x:Static autoUIsettings:AutoUISettings.Default}, Path=RunPeriod, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="35" x:Name="RunFor"
TextChanged="RunFor_TextChanged" KeyDown="TextBox_KeyDown" LostFocus="TextBox_LostFocus" />
<!--Change UI For Help-->
<Button Content="[?]" Command="{Binding ShowCHuiForHelp}" Margin="5" Foreground="DarkCyan" FontSize="10">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
<!--Set Max UI value-->
<StackPanel Orientation="Horizontal" Grid.Column="1" Margin="10,5">
<Label Content="Max UI Value:" />
<TextBox Text="{Binding Source={x:Static autoUIsettings:AutoUISettings.Default}, Path=MaxUI, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="35" x:Name="MaxUI"
TextChanged="MaxUI_TextChanged" KeyDown="TextBox_KeyDown" LostFocus="TextBox_LostFocus" />
<!--Max UI Value Help-->
<Button Content="[?]" Command="{Binding ShowMaxValueHelp}" Margin="5" Foreground="DarkCyan" FontSize="10">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
<!--Set Min UI value-->
<StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="1" Margin="10,5">
<Label Content="Min UI Value:" />
<TextBox Text="{Binding Source={x:Static autoUIsettings:AutoUISettings.Default}, Path=MinUI, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" MinWidth="35" x:Name="MinUI"
TextChanged="MinUI_TextChanged" KeyDown="TextBox_KeyDown" LostFocus="TextBox_LostFocus" />
<!--Min UI Value Help-->
<Button Content="[?]" Command="{Binding ShowMinValueHelp}" Margin="5" Foreground="DarkCyan" FontSize="10">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
<!--Enable/Disable Max Run Period-->
<CheckBox Content="Enable Max Run Period" IsChecked="{Binding Source={x:Static autoUIsettings:AutoUISettings.Default}, Path=RunPeriodEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" x:Name="RunPeriodCheckbox" Margin="10" Grid.Column="1" Grid.Row="2"/>
</Grid>
<!--If Max UI is Met-->
<StackPanel Grid.Row="1" DataContext="{StaticResource AutomaticUISettings}">
<StackPanel Orientation="Horizontal">
<!--Header-->
<Label Content="If Max UI value is met" />
<!--Max UI Value Met Help-->
<Button Content="[?]" Command="{Binding ShowMaxValueMetHelp}" Margin="5" Foreground="DarkCyan" FontSize="10">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
<!--Options-->
<RadioButton GroupName="MeetMaxUI" IsChecked="{Binding Path=Default.MaxMetRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Stop}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Stop" Margin="10,0" />
<RadioButton GroupName="MeetMaxUI" IsChecked="{Binding Path=Default.MaxMetRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=RollOver}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Roll over to zero" Margin="10,0" />
<RadioButton GroupName="MeetMaxUI" IsChecked="{Binding Path=Default.MaxMetRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=RollOvertoMin}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Roll over to Min UI value" Margin="10,0" />
</StackPanel>
<!--If Min UI is Met-->
<StackPanel Grid.Row="2" DataContext="{StaticResource AutomaticUISettings}">
<StackPanel Orientation="Horizontal">
<!--Header-->
<Label Content="If Min UI value is met" />
<!--Min UI Value Met Help-->
<Button Content="[?]" Command="{Binding ShowMinValueMetHelp}" Margin="5" Foreground="DarkCyan" FontSize="10">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<ContentPresenter />
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
<!--Options-->
<RadioButton GroupName="MeetMinUI" IsChecked="{Binding Path=Default.MinMetRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Stop}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Stop" Margin="10,0" />
<RadioButton GroupName="MeetMinUI" IsChecked="{Binding Path=Default.MinMetRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=RollOver}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Roll over to 255" Margin="10,0" />
<RadioButton GroupName="MeetMinUI" IsChecked="{Binding Path=Default.MinMetRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=RollOvertoMax}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Roll over to Max UI value" Margin="10,0" />
<RadioButton GroupName="MeetMinUI" IsChecked="{Binding Path=Default.MinMetRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=RolltoValue}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Roll over to calculated Value" Margin="10,0" />
<RadioButton GroupName="MeetMinUI" IsChecked="{Binding Path=Default.MinMetRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=InvertUI}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Change UI in inverse direction" Margin="10,0" />
</StackPanel>
<!--Max Run Period-->
<StackPanel Grid.Row="3" IsEnabled="{Binding Source={x:Static autoUIsettings:AutoUISettings.Default}, Path=RunPeriodEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
DataContext="{StaticResource AutomaticUISettings}">
<!--Header-->
<Label Content="If Run Period is met" />
<!--Options-->
<RadioButton GroupName="MeetRunPeriod" IsChecked="{Binding Path=Default.RunPeriodRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Stop}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Stop" Margin="10,0" />
<RadioButton GroupName="MeetRunPeriod" IsChecked="{Binding Path=Default.RunPeriodRadio, Mode=TwoWay, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Run}"
Command="{x:Static views:SetupAutoUI.CompareSettings}" Content="Continue to running at last UI" Margin="10,0" />
</StackPanel>
</Grid>
</GroupBox>
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Content="Apply" Command="{Binding ApplyAutoUI}" Padding="5" Margin="10,0" IsEnabled="{Binding AutoUIChanged}" />
<Button Content="Close/Cancel" Command="{Binding CloseAutoUI}" Padding="5" Margin="10,0" />
</StackPanel>
</Grid>