如何使用double数组的资源制作一个wpf组合框?

问题描述 投票:3回答:2

我的Window有一个double数组作为字段:

double[] sizes = new double[] {1.0, 2.0, 3.0, 4.0};

还有一个组合框,我希望它有精确的项目作为上面的数组。任何人都可以帮助我使用xaml绑定此数组作为组合框的资源?

.net wpf combobox binding
2个回答
7
投票

我不知道你的代码是什么样的,但这里是一个如何将数组绑定到ComboBox的示例

<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" Height="350" Width="525">
<Grid>
    <ComboBox ItemsSource="{Binding Sizes}" />
</Grid>
</Window>

代码背后:

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public double[] sizes = new double[] { 1.0, 2.0, 3.0, 4.0 };
        public double[] Sizes
        {
            get { return sizes; }
        }

        public MainWindow()
        {
            this.DataContext = this;   
        }
    }
}

1
投票

除了@ Nathan的回答之外,我想补充一点,如果您想要绑定到静态数据,出于性能原因将绑定模式指定为OneTime是个好主意:

<Grid>
    <ComboBox ItemsSource="{Binding Sizes, Mode=OneTime}" />
</Grid>

查看详情here

© www.soinside.com 2019 - 2024. All rights reserved.