用按钮扩展组合框

发布于 2024-10-28 22:19:39 字数 338 浏览 0 评论 0 原文

我想以在组合框旁边显示两个按钮的方式扩展 WPF 组合框。我无法使用 UserControl,因为我需要在纯 xaml 中指定组合框的项目,如下所示:

<CustomComboBox>
   <CustomComboBoxItem />
   <CustomComboBoxItem />
</CustomComboBox>

我非常害怕采用组合框的模板并扩展它,因为对于组合框来说它非常大且复杂。我正在寻找一种简单的解决方案来创建那种类似 ComboBox 的 ItemsControl ,只附加两个按钮。欢迎提出建议!

I want to extend the WPF Combobox in a way that two buttons are displayed next to the combobox. I cannot use a UserControl, because I need to specify the items of the combobox in pure xaml like this:

<CustomComboBox>
   <CustomComboBoxItem />
   <CustomComboBoxItem />
</CustomComboBox>

I'm quite scared to take the template of the combobox and extend it, because for comboboxes it is very large and complex. I'm looking for an easy and simple solution to create that kind of ComboBox-like ItemsControl with just two buttons attached to it. Suggestions welcome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£噩梦荏苒 2024-11-04 22:19:39

编辑:使用UserControl的具体示例:

Xaml:

<UserControl x:Class="Test.CustomComboBox"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel Orientation="Horizontal">
        <ComboBox Name="_comboBox" Margin="5"/>
        <Button Content="_Apply" Padding="3"  Margin="5" Click="Button_Apply_Click"/>
        <Button Content="_Reset" Padding="3"  Margin="5" Click="Button_Reset_Click"/>
    </StackPanel>
</UserControl>

代码:

[ContentProperty("Items")]
public partial class CustomComboBox : UserControl
{
    public event RoutedEventHandler ApplyClick;
    public event RoutedEventHandler ResetClick;

    public ItemCollection Items
    {
        get { return _comboBox.Items; }
        set
        {
            _comboBox.Items.Clear();
            foreach (var item in value)
            {
                _comboBox.Items.Add(item);
            }
        }
    }

    public CustomComboBox()
    {
        InitializeComponent();
    }

    private void Button_Apply_Click(object sender, RoutedEventArgs e)
    {
        if (ApplyClick != null)
        {
            ApplyClick(sender, e);
        }
    }

    private void Button_Reset_Click(object sender, RoutedEventArgs e)
    {
        if (ResetClick != null)
        {
            ResetClick(sender, e);
        }
    }
}

用法:

 <local:CustomComboBox ApplyClick="Button2_Click">
     <ComboBoxItem Content="Item1"/>
     <ComboBoxItem Content="Item2"/>
     <ComboBoxItem Content="Item3"/>
 </local:CustomComboBox>

外观:


UserControl 应该可以,您仍然可以在 Xaml 标记中指定项目,例如,如果我有一个时间用户控件,我可以这样做:

[ContentProperty("Hours")]
public partial class TimeBox : UserControl
{
    public string Hours
    {
        get { return this.TBHours.Text; }
        set { this.TBHours.Text = value; }
    }

    ...
}

这样您就可以在 XAML 中设置时间:

        <local:TimeBox>
            <sys:String>24</sys:String>
        </local:TimeBox>

您应该能够调整它来设置 ComboBox 的项目。

Edit: Conrete example using a UserControl:

Xaml:

<UserControl x:Class="Test.CustomComboBox"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <StackPanel Orientation="Horizontal">
        <ComboBox Name="_comboBox" Margin="5"/>
        <Button Content="_Apply" Padding="3"  Margin="5" Click="Button_Apply_Click"/>
        <Button Content="_Reset" Padding="3"  Margin="5" Click="Button_Reset_Click"/>
    </StackPanel>
</UserControl>

Code:

[ContentProperty("Items")]
public partial class CustomComboBox : UserControl
{
    public event RoutedEventHandler ApplyClick;
    public event RoutedEventHandler ResetClick;

    public ItemCollection Items
    {
        get { return _comboBox.Items; }
        set
        {
            _comboBox.Items.Clear();
            foreach (var item in value)
            {
                _comboBox.Items.Add(item);
            }
        }
    }

    public CustomComboBox()
    {
        InitializeComponent();
    }

    private void Button_Apply_Click(object sender, RoutedEventArgs e)
    {
        if (ApplyClick != null)
        {
            ApplyClick(sender, e);
        }
    }

    private void Button_Reset_Click(object sender, RoutedEventArgs e)
    {
        if (ResetClick != null)
        {
            ResetClick(sender, e);
        }
    }
}

Usage:

 <local:CustomComboBox ApplyClick="Button2_Click">
     <ComboBoxItem Content="Item1"/>
     <ComboBoxItem Content="Item2"/>
     <ComboBoxItem Content="Item3"/>
 </local:CustomComboBox>

Appearance:

enter image description here


A UserControl should do fine, you can still specify the items in Xaml markup, e.g. if i have a time user control i can do this:

[ContentProperty("Hours")]
public partial class TimeBox : UserControl
{
    public string Hours
    {
        get { return this.TBHours.Text; }
        set { this.TBHours.Text = value; }
    }

    ...
}

That way you can set the hours in XAML:

        <local:TimeBox>
            <sys:String>24</sys:String>
        </local:TimeBox>

You should be able to adapt this to set the items of your ComboBox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文