嵌套Stackpanel按钮不起作用

时间:2013-09-19 20:26:22

标签: wpf button stackpanel groupbox

我有一个嵌套在两个堆栈面板中的按钮,如此代码所示。

            <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>

整件事情是Grid GroupBox UserControl。 单击时,“Max UI Value Met Help”按钮不起作用。我在此Grid上方的StackPanel处有类似的帮助按钮,效果很好。我试图通过这种方式复制和粘贴它们,但它们也不起作用。

为什么这个按钮不起作用?

更新 我不得不切换到网格并分离标签&amp;从单选按钮堆栈按钮,以使其工作,但我想了解为什么这不起作用。

此处的按钮与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>

1 个答案:

答案 0 :(得分:0)

实际上你必须要理解Control的 ContentTemplate ControlTemplate 之间的差异(这里是Button)。

  • ContentTemplate 说明控制内容的外观如何保留控件的所有事件和属性。
  • ControlTemplate 说明你的控件是如何被覆盖的意味着它失去了它的所有属性和事件,所以你可以单独定义它们。意思是你实际上已经改变了控制。我认为你不想要。 / LI>

所以代替更改 ControlTemplate 更改其 ContentTemplate 就像这样。

<Button> 
 <Button.ContentTemplate>
  <DataTemplate> 

   <contentpresenter/>  // place code how your button should look like

 </DataTemplate> 
 </Button.ContentTemplate>
<Button>
相关问题