ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

c# – 扩展工具包SplitButton:使用ComboBox ItemsSource作为DropDownContent

2019-06-28 03:06:14  阅读:282  来源: 互联网

标签:c combobox wpf wpf-extended-toolkit split-button


我想重构我的代码,以便使用Extended Toolkit SplitButton代替Standard Combobox.

这是我最初的工作代码:

<ComboBox ItemsSource="{Binding Path=VisualizationList, Mode=TwoWay}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock
                    Text="{Binding Converter={StaticResource MultiLangConverter}/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

这是我的SplitButton

<xctk:SplitButton Content="Click Me">
    <xctk:SplitButton.DropDownContent>
        <!-- What should I use here? -->
    </xctk:SplitButton.DropDownContent>
</xctk:SplitButton>

现在,我想将ItemsSource之类的东西设置为SplitButton,以使其行为类似于标准的ComboBox按钮.
有没有办法得到这样的行为?

如果你需要它,这里是我的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>

解决方法:

一种方法是添加ListBox作为SplitButton的内容.

您必须根据需要添加自定义代码才能获得所需的确切行为,但这是一个让您入门的示例:

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

标签:c,combobox,wpf,wpf-extended-toolkit,split-button
来源: https://codeday.me/bug/20190628/1311380.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有