2013-07-22 41 views
2

我希望能夠通過使用枚舉來訪問XAML中的ObservableCollection中的項目。使用枚舉訪問xaml中的數組

我可以綁定到的ObservableCollection,並指定哪個項目在XAML以下列方式:

<Window x:Class="ArrayPropertyBinding.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:ArrayPropertyBinding" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.DataContext> 
     <local:MyViewModel /> 
    </Window.DataContext> 
    <StackPanel> 
     <CheckBox Content="First" IsChecked="{Binding FilterBy[0],Mode=TwoWay}"/> 
     <CheckBox Content="Second" IsChecked="{Binding FilterBy[1],Mode=TwoWay}"/> 
     <CheckBox Content="Thrid" IsChecked="{Binding FilterBy[2],Mode=TwoWay}"/> 
     <CheckBox Content="Fourth" IsChecked="{Binding FilterBy[3],Mode=TwoWay}"/> 
     <Button Content="Print" Click="Button_Click_1"/> 
     <Button Content="SetFourthTrue" Click="Button_Click_SetFourthTrue"/> 
    </StackPanel> 
</Window> 

雖然視圖模型看起來像這樣

public class MyViewModel 
{ 
    public enum Filters 
    { 
     First = 0, 
     Second, 
     Thrid, 
     Fourth 
    } 

    private ObservableCollection<bool> _filterBy = new ObservableCollection<bool>() { false, false, false, false }; 
    public ObservableCollection<bool> FilterBy 
    { 
     get { return _filterBy; } 
    } 

    public void PrintFilters() 
    { 
     System.Diagnostics.Debug.Write("<<<<"); 
     foreach (bool b in _filterBy) 
     { 
      System.Diagnostics.Debug.Write(b); 
      System.Diagnostics.Debug.Write(" "); 
     } 
     System.Diagnostics.Debug.WriteLine(">>>>"); 
    } 

    public void SetFourthTrue() 
    { 
     FilterBy[(int)Filters.Fourth] = true; 
    } 
} 

我想能夠寫XAML爲:

<CheckBox Content="First" IsChecked="{Binding FilterBy[Filters.First],Mode=TwoWay}"/> 

任何指針或想法將不勝感激。

回答

3

可能歸結爲custom binding path construction,因爲試圖直接輸入一個enum值似乎不可能。然後

語法是:

{Binding Path={me:PathConstructor FilterBy[(0)], {x:Static myns:Filters.First}}} 

(會推薦移動枚舉出來的類,否則就myns:MyViewModel+Filters.First如果我沒有記錯的話)

+0

真棒。總共+10。 –

+0

謝謝H.這讓我朝着正確的方向前進。我在我的問題中發佈了我的更新代碼。 – AlC

+0

@AIC:請不要*回答問題,請將它作爲您自己的答案張貼,因爲Q和A應嚴格分開。 –

1

所以,這是我HB的答案後, 。它對我來說很好。

<StackPanel> 
    <CheckBox Content="First" IsChecked="{Binding Path={local:PathConstructor FilterBy, {x:Static local:Filters.First}}}"/> 
    <CheckBox Content="Second" IsChecked="{Binding Path={local:PathConstructor FilterBy, {x:Static local:Filters.Second}}}"/> 
    <CheckBox Content="Thrid" IsChecked="{Binding FilterBy[2],Mode=TwoWay}"/> 
    <CheckBox Content="Fourth" IsChecked="{Binding FilterBy[3],Mode=TwoWay}"/> 
    <Button Content="Print" Click="Button_Click_1"/> 
    <Button Content="SetFourthTrue" Click="Button_Click_SetFourthTrue"/> 
</StackPanel> 

而且在後面的代碼

public static class StringEnumConversion 
{ 
    public static int ConvertToEnum<T>(object value) 
    { 
     Contract.Requires(typeof(T).IsEnum); 
     Contract.Requires(value != null); 
     Contract.Requires(Enum.IsDefined(typeof(T), value.ToString())); 
     return (int)Enum.Parse(typeof(T), value.ToString()); 
    } 
} 

[ContentProperty("Parameters")] 
public class PathConstructor : MarkupExtension 
{ 
    public string Path { get; set; } 
    public IList Parameters { get; set; } 

    public PathConstructor() 
    { 
     Parameters = new List<object>(); 
    } 

    public PathConstructor(string b, object p0) 
    { 
     Path = b; 
     Parameters = new[] { p0 }; 
    } 

    public override object ProvideValue(IServiceProvider serviceProvider) 
    { 
     return new PropertyPath(String.Format("{0}[{1}]",Path,StringEnumConversion.ConvertToEnum<Filters>(Parameters[0]))); 
    } 
}