我是XAML的新手,WPF(第一个项目;-))。我的目标是应用App.xaml中声明的自定义组合框。但是,如果我然后在MainWindow.xaml中使用组合框,我不知道如何将单击处理程序附加到组合框中锚定的togglebutton。如果我将一个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>
答案 0 :(得分:-1)
使用第一个XAML块,您将添加一个ToggleButton作为ComboBox的子级。所以你可以用这种方式循环所有项目:
foreach(ToggleButton btn in someComboBox.Items)
{
btn.Click += event_handler;
}
但我不明白你需要什么! : - )