【问题标题】:WPF Combobox with string Bind to Int property带有字符串绑定到 Int 属性的 WPF 组合框
【发布时间】:2013-09-01 18:58:39
【问题描述】:

我想要一个数字为 1-8 的组合框,并将所选值绑定到 int 类型的属性“NumberOfZones”。默认情况下,组合框返回字符串值,因此不能将其保存在 int 属性中。如何将其类型转换为 int。

如何设置项目并在 int 中进行选择。

   <ComboBox Background="#FFB7B39D" Height="23" Name="cboNumZones" Width="74" Margin="158,16,368,247" Grid.Row="2" SelectionChanged="cboNumZones_SelectionChanged" 
    SelectedValue="{Binding Path=NumberOfZones, Mode=TwoWay}">
   </ComboBox>
                <!--
                <ComboBoxItem >1</ComboBoxItem>
                    <ComboBoxItem >2</ComboBoxItem>
                    <ComboBoxItem >3</ComboBoxItem>
                    <ComboBoxItem >4</ComboBoxItem>
                    <ComboBoxItem >5</ComboBoxItem>
                    <ComboBoxItem >6</ComboBoxItem>
                    <ComboBoxItem >7</ComboBoxItem>
                    <ComboBoxItem >8</ComboBoxItem>
                -->

包含 NumberOfZones 属性的对象是 UserControl 的 DataContext。

非常感谢。

【问题讨论】:

    标签: c# wpf string combobox int


    【解决方案1】:

    您可以将ItemsSource 设置为int 数组,然后SelectedItem 将是int32 类型:

    <ComboBox SelectedItem="{Binding Path=NumberOfZones, Mode=TwoWay}">             
       <ComboBox.ItemsSource>
          <x:Array Type="{x:Type sys:Int32}">
             <sys:Int32>1</sys:Int32>
             <sys:Int32>2</sys:Int32>
             <sys:Int32>3</sys:Int32>
             <sys:Int32>4</sys:Int32>
             <sys:Int32>5</sys:Int32>
             <sys:Int32>6</sys:Int32>
             <sys:Int32>7</sys:Int32>
             <sys:Int32>8</sys:Int32>
          </x:Array>
       </ComboBox.ItemsSource>
    </ComboBox>
    

    为此,您需要将 sys: 命名空间添加到您的 XAML:

    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    

    【讨论】:

    • 感谢您的回复。 Sheridan 的解决方案更适合我的应用程序。谢谢。
    • 如果程序是 64 位构建的,这仍然有效吗?
    【解决方案2】:

    您对 ComboBox 返回的内容有误。您的返回字符串值,因为那是您放入的内容。相反,如果您创建一个声明了 NumberOfZones 属性的属性:

    public ObservableCollection<int> Numbers { get; set; }
    

    然后将其数据绑定到您的ComboBox

    <ComboBox ItemSource="{Binding Numbers}" Background="#FFB7B39D" Height="23" 
        Name="cboNumZones" Width="74" Margin="158,16,368,247" Grid.Row="2" 
        SelectionChanged="cboNumZones_SelectionChanged" SelectedValue="{
        Binding Path=NumberOfZones, Mode=TwoWay}">
    

    那么您选择的号码也将是int

    【讨论】:

    • 谢里丹,我错了。是的,您的代码按照您所说的完成了工作,甚至两种方式都成功了。这个解决方案解决了我的这个问题。我的另一个问题 - 我想保持 Numbers 属性通用,因为它在应用程序的其他地方使用。我无法实现这种绑定。我在这里分享了它-stackoverflow.com/questions/18493749/…。请查看您是否可以建议这样的酷和最佳选择。
    【解决方案3】:

    我知道这个问题是针对 WPF 的,但如果您在 Windows 8.1(WinRT、通用应用程序)上寻找答案,那就是:

    <ComboBox SelectedItem="{Binding NumberOfZones, Mode=TwoWay}">
        <x:Int32>1</x:Int32>
        <x:Int32>2</x:Int32>
        <x:Int32>3</x:Int32>
        <x:Int32>4</x:Int32>
        <x:Int32>5</x:Int32>
    </ComboBox>
    

    考虑到

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 2016-12-10
      • 2012-05-05
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 2014-11-24
      相关资源
      最近更新 更多