在WPF中绑定到属性指定的数组元素

its*_*att 28 data-binding wpf xaml

假设我的UI上有一些TextBlocks,就像这样:

<StackPanel Orientation="Vertical">
    <TextBlock Text="{Binding DessertIndex}" />
    <TextBlock Text="{Binding Food[2]}" />
    <TextBlock Text="{Binding Food[{Binding DessertIndex}]}" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

在我的代码后面我有这样的东西:

public partial class MainWindow : Window
{
    public int DessertIndex
    {
        get { return 2; }
    }

    public object[] Food
    {
        get
        {
            return new object[]{"liver", "spam", "cake", "garlic" };
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
    }
}
Run Code Online (Sandbox Code Playgroud)

前两个TextBlocks对我来说很好,分别显示2和'cake'.第三个没有完成我想要的,即使用DessertIndex属性索引到该数组并显示'cake'.我在这里搜索了一些关于类似问题但没有找到的问题.最终,我不想在我的.xaml文件中指定像2这样的值,而是希望依赖属性来索引到该数组中.这可能吗?如果是这样,我在这里做错了什么?


编辑:

所以我更接近的是数据是这些object []的List的情况,我使用上面的StackPanel作为ListBox的DataTemplate的一部分.因此,正如Mark Heath在下面所建议的那样,使用取消引用数组的属性的想法似乎并不像我想要的那样工作.想法?

Col*_*sen 30

另一种方法是将MultiBinding与转换器一起使用:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Orientation="Vertical">
        <StackPanel.Resources>
            <local:FoodIndexConverter x:Key="foodIndexConverter" />
        </StackPanel.Resources>
        <TextBlock Text="{Binding DessertIndex}" />
        <TextBlock Text="{Binding Food[2]}" />
        <TextBlock>
                <TextBlock.Text>
                    <MultiBinding Converter="{StaticResource foodIndexConverter}">
                        <Binding Path="DessertIndex" />
                        <Binding Path="Food"/>
                    </MultiBinding>
                </TextBlock.Text>
        </TextBlock>
    </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

然后在代码隐藏中,转换器定义如下:

namespace WpfApplication1
{
    public class FoodIndexConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values == null || values.Length != 2)
                return null;

            int? idx = values[0] as int?;
            object[] food = values[1] as object[];

            if (!idx.HasValue || food == null)
                return null;

            return food[idx.Value];
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢 Colin - 这对我的测试应用程序非常有效,并且怀疑它在真实情况下也能正常工作。这是美好的一天 - 我从你那里学到了一些很酷的东西。:) (2认同)
  • 您可能想尝试使用ConverterParameter来尝试不同的东西以避免多重绑定.不幸的是,这将无处可去,因为ConverterParameter不能使用绑定,因为它不是DependencyProperty,你必须使用MultiBinding (2认同)

Mar*_*ath 13

如果你DesertIndex在使用DataContext上有一个属性的麻烦,为什么不使用DesertIndex取消引用Food数组的属性:

public object SelectedFood
{
    get { return Food[DessertIndex]; }
}    

public int DessertIndex
{
    get { return 2; }
}

public object[] Food
{
    get
    {
        return new object[]{"liver", "spam", "cake", "garlic" };
    }
}
Run Code Online (Sandbox Code Playgroud)

然后你可以直接绑定到:

<TextBlock Text="{Binding SelectedFood}" />
Run Code Online (Sandbox Code Playgroud)

这本质上是"MVVM"方法:使datacontext对象具有适合绑定的属性.