【问题标题】:Toggle button (inside combobox) attach click handler切换按钮(在组合框内)附加点击处理程序
【发布时间】:2016-07-18 16:33:38
【问题描述】:

我是 XAML、WPF 的新手(第一个项目 ;-))。我的目标是应用在 App.xaml 中声明的自定义组合框。但是,如果我随后使用 MainWindow.xaml 中的组合框,我不知道如何将单击处理程序附加到锚定在组合框内的切换按钮。如果我将 Click 属性添加到 App.xaml 中的 ToggleButton,则只有在 App.cs 中实现处理程序时才能成功捕获事件。

到目前为止我已经尝试过:

  • 请参阅 MainWindow.xaml 中 ComboBox 元素内的 ToggleButton(我目前对 xaml 的工作方式没有很好的直觉......),这会导致 ToggleButton 上出现一些不希望的视觉效果,仅此而已。

    <ComboBox x:Name="someComboBox" 
              Height="23"
              Margin="122,35,1.8,0"
              VerticalAlignment="Top">
    
             <ToggleButton Click="toggleHandler_Click" />
    
  • 从 MainWindow.cs 中获取 ToggleButton,然后附加处理程序,尽管前者采用以下方法导致 null

        ToggleButton tBtn = ((ComboBox)someComboBox).FindName("MyToggleButton") as ToggleButton; // null
    
        ToggleButton tBtn = LogicalTreeHelper.FindLogicalNode(someComboBox, "MyToggleButton"); // null
    

这里是 App.xaml 中的自定义组合框,我照原样采用

        <Style TargetType="ComboBox">
            <Setter Property="Validation.ErrorTemplate" Value="{x:Null}" />
            <Setter Property="VerticalAlignment" Value="Center" />
            <Setter Property="OverridesDefaultStyle" Value="true" />
            <Setter Property="IsEditable" Value="True"/>
            <Setter Property="SnapsToDevicePixels" Value="true" />
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
            <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
            <Setter Property="ScrollViewer.CanContentScroll" Value="true" />
            <Setter Property="Margin" Value="2" />
            <Setter Property="MinHeight" Value="20" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ComboBox">
                        <Grid>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver"/>
                                    <VisualState x:Name="Disabled"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <ToggleButton x:Name="MyToggleButton"
                                          Grid.Column="2"
                                          ClickMode="Press"
                                          Focusable="false"
                                          IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"
                                          Template="{StaticResource ComboBoxToggleButton}"/>

                            <ContentPresenter Margin="3,3,23,3" 
                                              Content="{TemplateBinding SelectionBoxItem}"
                                              ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}"
                                              ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"
                                              HorizontalAlignment="Left"
                                              IsHitTestVisible="False"
                                              x:Name="ContentSite"
                                              VerticalAlignment="Center" />

                            <TextBox Style="{x:Null}" 
                                     x:Name="PART_EditableTextBox" 
                                     Margin="3,3,23,3" 
                                     Background="Transparent"
                                     Focusable="True"
                                     HorizontalAlignment="Left"
                                     IsReadOnly="{TemplateBinding IsReadOnly}"
                                     Template="{StaticResource ComboBoxTextBox}" 
                                     VerticalAlignment="Center" 
                                     Visibility="Hidden" />

                            <Popup AllowsTransparency="True" Focusable="False" IsOpen="{TemplateBinding IsDropDownOpen}" x:Name="Popup" Placement="Bottom" PopupAnimation="Fade">
                                <Grid MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{TemplateBinding ActualWidth}" x:Name="DropDown" SnapsToDevicePixels="True">
                                    <Border x:Name="DropDownBorder" Background="White" BorderBrush="{StaticResource BorderDarkMainBrush}" BorderThickness="1" CornerRadius="0" />
                                    <ScrollViewer Margin="2" SnapsToDevicePixels="True">
                                        <StackPanel KeyboardNavigation.DirectionalNavigation="Contained" IsItemsHost="True" TextBlock.Foreground="Black" />
                                    </ScrollViewer>
                                </Grid>
                            </Popup>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="HasItems" Value="false">
                                <Setter Property="MinHeight" TargetName="DropDownBorder" Value="95" />
                            </Trigger>
                            <Trigger Property="IsGrouping" Value="true">
                                <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
                            </Trigger>
                            <Trigger Property="IsEditable" Value="true">
                                <Setter Property="IsTabStop" Value="false" />
                                <Setter Property="Visibility" TargetName="PART_EditableTextBox" Value="Visible" />
                                <Setter Property="Visibility" TargetName="ContentSite" Value="Hidden" />
                            </Trigger>
                        </ControlTemplate.Triggers>

                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="IsFocused" Value="True">
                    <Setter Property="Background" Value="Transparent" />
                </Trigger>
            </Style.Triggers>
        </Style>

【问题讨论】:

  • 当您运行应用程序并单击 ComboBox 内的按钮时,您会收到任何错误吗?
  • 不,我没有收到任何错误。只是不知道捕获事件的最佳实践是什么。如果我在 App.xaml 中的 ToggleButton 中添加 Click 属性,那么在 App.cs 中处理 click 事件是没有问题的。我仍然想在 MainWindow 的上下文中处理事件。

标签: c# wpf xaml combobox togglebutton


【解决方案1】:

使用您的第一个 XAML 块,您将添加一个 ToggleButton 作为 ComboBox 的子项。所以你可以这样循环所有的项目:

foreach(ToggleButton btn in someComboBox.Items)
{
   btn.Click += event_handler;
}

但我不明白你到底需要什么! :-)

【讨论】:

  • 尝试了您的建议并在“btn”上获得了“InvalidCastException”。此外,当我浏览“someComboBox”对象的成员时,我无法发现 ToggleButton。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
相关资源
最近更新 更多