【WPF】ComboBox:根据绑定选取、设置固定集合中的值

在WPF中,当需要限制用户从ComboBox中选择特定值(例如5、10、15、20秒)时,可以通过结合使用ItemStringFormat和SelectedValue来实现。通过ItemStringFormat可以自定义显示文本,如'10秒',而SelectedValue则用于获取选定的数值。虽然初看不需要设置ItemsSource,但最终解决方案仍需要返回到ItemsSource,并确保删除手动添加元素的代码。

问题场景

我有一个对象,里面有一个属性叫Limit,int类型。虽然int可取的范围很大,我想要在用户界面上限制Limit可取的值,暂且限制为5、10、15、20。

所以ComboBox绑定不是绑定常见的ItemsSource(至少初看起来不是),而是Text、SelectedItem、SelectedValue或是什么东西,先卖个关子。

另外,Limit是表示时间的,单位秒。我要求ComboBox上能显示10秒、5秒,而不是光秃秃的10和5。

解决方案

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow"  SizeToContent="WidthAndHeight">
    <StackPanel Margin="10">
        <ComboBox Name="comboBox"  ItemStringFormat="{}{0}秒" SelectedValue="{Binding Limit, RelativeSource={RelativeSource AncestorType=Window}}" />
        <Button Content="读" Click="Button_Click" />
    </StackPanel>
</Window>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            Limit = 10;

            InitializeComponent();

            comboBox.Items.Add(10);
            comboBox.Items.Add(15);
            comboBox.Items.Add(20);
            comboBox.Items.Add(25);

        }

        public int Limit { get; set; }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //此处下断点来看Limit到底变了没有。
        }
    }

小结:使用ItemStringFormat来加上“秒”字,用SelectedValue选取值。

能不能用纯XAML完成呢?

     <ComboBox  Name="comboBox"  ItemStringFormat="{}{0}秒" SelectedValue="{Binding Limit, RelativeSource={RelativeSource AncestorType=Window}}">
            <ComboBoxItem>
                <System:Int32>10</System:Int32>
            </ComboBoxItem>
            <ComboBoxItem>
                <System:Int32>15</System:Int32>
            </ComboBoxItem>
            <ComboBoxItem>
                <System:Int32>20</System:Int32>
            </ComboBoxItem>
            <ComboBoxItem>
                <System:Int32>25</System:Int32>
            </ComboBoxItem>
        </ComboBox>

以上方案是不行的!因为ComboBox的集合已经是ComboBoxItem,而不是int了。所以还得回归ItemsSource。

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:System="clr-namespace:System;assembly=mscorlib"
        Title="MainWindow"  SizeToContent="WidthAndHeight">
    <StackPanel Margin="10">
        <StackPanel.Resources>
            <x:Array x:Key="candidateValues" Type="System:Int32">
                <System:Int32>10</System:Int32>
                <System:Int32>15</System:Int32>
                <System:Int32>20</System:Int32>
            </x:Array>
        </StackPanel.Resources>
        <ComboBox  Name="comboBox"  ItemStringFormat="{}{0}秒" ItemsSource="{StaticResource candidateValues}" SelectedValue="{Binding Limit, RelativeSource={RelativeSource AncestorType=Window}}">
        </ComboBox>
        <Button Content="读" Click="Button_Click" />
    </StackPanel>
</Window>

如上即可,记得删除MainWindow构造函数里给comboBox加元素的语句。


本文以知识共享-署名-相同方式共享方式发布

作者爱让一切都对了


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值