C# 扩展工具包拆分按钮:使用组合框项资源作为下拉内容

C# 扩展工具包拆分按钮:使用组合框项资源作为下拉内容,c#,wpf,combobox,split-button,wpf-extended-toolkit,C#,Wpf,Combobox,Split Button,Wpf Extended Toolkit,我想重构我的代码,以便使用而不是标准的Combobox 以下是我的初始工作代码: <ComboBox ItemsSource="{Binding Path=VisualizationList, Mode=TwoWay}"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel> <TextBlock

我想重构我的代码,以便使用而不是标准的
Combobox

以下是我的初始工作代码:

<ComboBox ItemsSource="{Binding Path=VisualizationList, Mode=TwoWay}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock
                    Text="{Binding Converter={StaticResource MultiLangConverter}/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>
现在,我想将类似
ItemsSource
的内容设置为
SplitButton
,以使其行为类似于标准的组合框按钮。 有没有办法得到这种行为

如果需要,这里是我的
SplitButton
ControlTemplate
样式

<ControlTemplate x:Key="SplitButtonTemplate" TargetType="xctk:SplitButton">
<Grid x:Name="MainGrid" SnapsToDevicePixels="True">
    <xctk:ButtonChrome x:Name="ControlChrome" Background="{TemplateBinding Background}"
                       RenderEnabled="{TemplateBinding IsEnabled}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Button x:Name="PART_ActionButton"
                    Margin="0"
                    HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                    VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                    Padding="{TemplateBinding Padding}"
                    Tag="{TemplateBinding Tag}"
                    Style="{StaticResource BlackButton}">
                <ContentPresenter Name="ActionButtonContent" Margin="{TemplateBinding Padding}"
                                          Content="{TemplateBinding Content}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          RecognizesAccessKey="true" />
            </Button>
            <ToggleButton x:Name="PART_ToggleButton"
                          Grid.Column="1"
                          IsTabStop="False"
                          IsChecked="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}">
                <ToggleButton.Template>
                    <ControlTemplate TargetType="ToggleButton">
                        <ContentPresenter />
                    </ControlTemplate>
                </ToggleButton.Template>
                <Grid>
                    <xctk:ButtonChrome x:Name="ToggleButtonChrome"
                                       RenderNormal="False"
                                       RenderChecked="{TemplateBinding IsOpen}"
                                       RenderEnabled="{TemplateBinding IsEnabled}"
                                       RenderMouseOver="{Binding IsMouseOver, ElementName=PART_ToggleButton}"
                                       RenderPressed="{Binding IsPressed, ElementName=PART_ToggleButton}">
                        <Grid x:Name="arrowGlyph" IsHitTestVisible="False" Margin="15,6,15,6">
                            <Path Width="7" Height="4"
                                  Data="M 0,1 C0,1 0,0 0,0 0,0 3,0 3,0 3,0 3,1 3,1 3,1 4,1 4,1 4,1 4,0 4,0 4,0 7,0 7,0 7,0 7,1 7,1 7,1 6,1 6,1 6,1 6,2 6,2 6,2 5,2 5,2 5,2 5,3 5,3 5,3 4,3 4,3 4,3 4,4 4,4 4,4 3,4 3,4 3,4 3,3 3,3 3,3 2,3 2,3 2,3 2,2 2,2 2,2 1,2 1,2 1,2 1,1 1,1 1,1 0,1 0,1 z"
                                      Fill="White" />
                            </Grid>
                        </xctk:ButtonChrome>
                    </Grid>
                </ToggleButton>
            </Grid>
        </xctk:ButtonChrome>
        <Popup IsOpen="{Binding IsChecked, ElementName=PART_ToggleButton}"
               AllowsTransparency="True"
               PopupAnimation="Slide">
            <Grid x:Name="DropDown"
                  MinWidth="{TemplateBinding ActualWidth}"
                  MaxHeight="{TemplateBinding MaxDropDownHeight}">
                <Border x:Name="DropDownBorder" BorderBrush="{StaticResource StandardBorderColor}"
                        Background="{StaticResource ComboBoxBackgroundColor}" />
                <ScrollViewer>
                    <ContentPresenter Content="{TemplateBinding DropDownContent}" />
                </ScrollViewer>
            </Grid>
        </Popup>
    </Grid>
</ControlTemplate>
<Style TargetType="xctk:SplitButton">
    <Setter Property="Template" Value="{StaticResource SplitButtonTemplate}" />
</Style>

一种方法是添加一个
列表框作为
拆分按钮的内容

<ControlTemplate x:Key="SplitButtonTemplate" TargetType="xctk:SplitButton">
<Grid x:Name="MainGrid" SnapsToDevicePixels="True">
    <xctk:ButtonChrome x:Name="ControlChrome" Background="{TemplateBinding Background}"
                       RenderEnabled="{TemplateBinding IsEnabled}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Button x:Name="PART_ActionButton"
                    Margin="0"
                    HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                    VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                    Padding="{TemplateBinding Padding}"
                    Tag="{TemplateBinding Tag}"
                    Style="{StaticResource BlackButton}">
                <ContentPresenter Name="ActionButtonContent" Margin="{TemplateBinding Padding}"
                                          Content="{TemplateBinding Content}"
                                          ContentTemplate="{TemplateBinding ContentTemplate}"
                                          VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                          RecognizesAccessKey="true" />
            </Button>
            <ToggleButton x:Name="PART_ToggleButton"
                          Grid.Column="1"
                          IsTabStop="False"
                          IsChecked="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}">
                <ToggleButton.Template>
                    <ControlTemplate TargetType="ToggleButton">
                        <ContentPresenter />
                    </ControlTemplate>
                </ToggleButton.Template>
                <Grid>
                    <xctk:ButtonChrome x:Name="ToggleButtonChrome"
                                       RenderNormal="False"
                                       RenderChecked="{TemplateBinding IsOpen}"
                                       RenderEnabled="{TemplateBinding IsEnabled}"
                                       RenderMouseOver="{Binding IsMouseOver, ElementName=PART_ToggleButton}"
                                       RenderPressed="{Binding IsPressed, ElementName=PART_ToggleButton}">
                        <Grid x:Name="arrowGlyph" IsHitTestVisible="False" Margin="15,6,15,6">
                            <Path Width="7" Height="4"
                                  Data="M 0,1 C0,1 0,0 0,0 0,0 3,0 3,0 3,0 3,1 3,1 3,1 4,1 4,1 4,1 4,0 4,0 4,0 7,0 7,0 7,0 7,1 7,1 7,1 6,1 6,1 6,1 6,2 6,2 6,2 5,2 5,2 5,2 5,3 5,3 5,3 4,3 4,3 4,3 4,4 4,4 4,4 3,4 3,4 3,4 3,3 3,3 3,3 2,3 2,3 2,3 2,2 2,2 2,2 1,2 1,2 1,2 1,1 1,1 1,1 0,1 0,1 z"
                                      Fill="White" />
                            </Grid>
                        </xctk:ButtonChrome>
                    </Grid>
                </ToggleButton>
            </Grid>
        </xctk:ButtonChrome>
        <Popup IsOpen="{Binding IsChecked, ElementName=PART_ToggleButton}"
               AllowsTransparency="True"
               PopupAnimation="Slide">
            <Grid x:Name="DropDown"
                  MinWidth="{TemplateBinding ActualWidth}"
                  MaxHeight="{TemplateBinding MaxDropDownHeight}">
                <Border x:Name="DropDownBorder" BorderBrush="{StaticResource StandardBorderColor}"
                        Background="{StaticResource ComboBoxBackgroundColor}" />
                <ScrollViewer>
                    <ContentPresenter Content="{TemplateBinding DropDownContent}" />
                </ScrollViewer>
            </Grid>
        </Popup>
    </Grid>
</ControlTemplate>
<Style TargetType="xctk:SplitButton">
    <Setter Property="Template" Value="{StaticResource SplitButtonTemplate}" />
</Style>
您必须根据需要添加自定义代码以获得所需的确切行为,但以下是一个示例:

<xctk:SplitButton x:Name="btnSplit" Content="Select a product...">
    <xctk:SplitButton.DropDownContent>
        <ListBox ItemsSource="..."
                 SelectionChanged="ListBox_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding ProductName}" />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </xctk:SplitButton.DropDownContent>
</xctk:SplitButton>

private void ListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    ListBox content = (ListBox)sender;
    btnSplit.Content = ((DataRowView)content.SelectedItem)["ProductName"].ToString();
    btnSplit.IsOpen = false;
}

私有无效列表框\u选择已更改(对象发件人,System.Windows.Controls.SelectionChangedEventArgs e)
{
列表框内容=(列表框)发送方;
btnSplit.Content=((DataRowView)Content.SelectedItem)[“ProductName”].ToString();
btnSplit.IsOpen=false;
}