2010-11-04 58 views
25

說指定數組的元素我有我的UI部分的TextBlocks,像這樣的東西:在WPF綁定到地產

<StackPanel Orientation="Vertical"> 
    <TextBlock Text="{Binding DessertIndex}" /> 
    <TextBlock Text="{Binding Food[2]}" /> 
    <TextBlock Text="{Binding Food[{Binding DessertIndex}]}" /> 
</StackPanel> 

,並在我的代碼後面我有這樣的事情:

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; 
    } 
} 

前兩個TextBlocks對我來說很好,分別顯示2和'cake'。第三個不完成我想要的,即使用DessertIndex屬性來索引該數組,並顯示'蛋糕'。我在這裏搜索了一個類似的問題,但沒有找到。最終,我不想在我的.xaml文件中指定像2那樣的值,並且想要依靠屬性來索引該數組。這可能嗎?如果是這樣,我在這裏做錯了什麼?


編輯:

所以我更加仔細地是數據爲這些對象[]列表的情況,我在上面使用的StackPanel中作爲一個ListBox一個DataTemplate的一部分。所以這個想法,正如Mark Heath在下面提出的那樣,使用一個解引用數組的屬性似乎並不像我想要的那樣工作。想法?

回答

26

另一種方法是使用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> 

然後在後臺代碼,轉換器定義是這樣的:

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(); 
     } 
    } 
} 
+0

感謝科林 - 這對我的測試應用程序非常有效,並且懷疑它在真實情況下也能正常工作。今天過得很愉快 - 我從你身上學到了很酷的東西。 :) – itsmatt 2010-11-04 20:42:51

+2

您可能會嘗試使用ConverterParameter嘗試不同的方法來避免多重綁定。不幸的是,這將無處可去,因爲ConverterParameter無法使用綁定,因爲它不是DependencyProperty,並且您必須使用MultiBinding – 2013-10-09 09:56:29

10

,如果你要對你的DataContext,有DesertIndex財產的麻煩,爲什麼不取消引用糧食陣列DesertIndex屬性:

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

public int DessertIndex 
{ 
    get { return 2; } 
} 

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

那麼你可以直接綁定到:

<TextBlock Text="{Binding SelectedFood}" /> 

這實質上就是「MVVM」方法:使datacontext對象具有適合綁定的屬性。

+0

馬克,感謝您的答覆。有關更多詳細信息,請參閱我對原始問題的編輯。基本上,我正在處理一個ListBox和這些對象的列表[]的,所以我不知道如何在這個上下文中建議工作。數據不是我的,我必須照原樣處理。想法? – itsmatt 2010-11-04 20:16:07

+0

是的,我確實懷疑這是否可能。 – 2010-11-04 20:28:09