2012-10-02 9 views
5

Ich bin eine Anwendung, und ich möchte einige Textfelder und Chekcboxes Wertfeld von Dictionary (Enum, String) binden. Ist das möglich und wie kann ich das machen?Bindung an Wert in Dictionary mit Enum als Schlüssel

in XAML-Code Ich habe so etwas wie diese - es wird als ein Schlüssel mit Schnur für Wörterbuch zu arbeiten, aber es kann Schlüssel nicht richtig binden mit Enum

<dxe:TextEdit EditValue="{Binding Properties[PrimaryAddress], Mode=TwoWay}" /> 
<dxe:TextEdit EditValue="{Binding Properties[SecondaryAddress], Mode=TwoWay}" /> 
<dxe:CheckEdit EditValue="{Binding Properties[UsePrimaryAddress], Mode=TwoWay}" /> 

.. und hier ist das, was ich in Enum habe

public Dictionary<MyEnum, string> Properties 

ich habe solut gefunden:

public enum MyEnum 
{ 
    PrimaryAddress, 
    SecondaryAddress, 
    UsePrimaryAddress 
} 

In Ansichtsmodell Wörterbuch ist definiert als Ion für Combobox mit Enum-Werten, aber das gilt nicht für meinen Fall.

Irgendwelche Ratschläge?

Antwort

9

Sie müssen den entsprechenden Typ für den Parameter des Indexers im Bindungsausdruck festlegen.

anzeigen Modell:

public enum Property 
{ 
    PrimaryAddress, 
    SecondaryAddress, 
    UsePrimaryAddress 
} 

public class ViewModel 
{ 
    public ViewModel() 
    { 
     Properties = new Dictionary<Property, object> 
     { 
      { Property.PrimaryAddress, "123" }, 
      { Property.SecondaryAddress, "456" }, 
      { Property.UsePrimaryAddress, true } 
     }; 
    } 

    public Dictionary<Property, object> Properties { get; private set; } 
} 

XAML:

<Window x:Class="WpfApplication5.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:WpfApplication5" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition/> 
      <RowDefinition/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 

     <TextBox Grid.Row="0" Text="{Binding Path=Properties[(local:Property)PrimaryAddress]}"/> 
     <TextBox Grid.Row="1" Text="{Binding Path=Properties[(local:Property)SecondaryAddress]}"/> 
     <CheckBox Grid.Row="2" IsChecked="{Binding Path=Properties[(local:Property)UsePrimaryAddress]}"/> 
    </Grid> 
</Window> 

-Code-behind:

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = new ViewModel(); 
    } 
} 

Weitere Informationen finden Sie unter "Binding Path Syntax".

+0

Verwendung der oben genannten Bindungspfad Ich habe folgenden Fehler: System.Windows.Data Fehler: 40: BindingExpression Pfad Fehler: '[]' Eigenschaft nicht gefunden 'Objekt' '' Dictionary'2 '(HashCode = 56465364)'. BindingExpression: Pfad = Eigenschaften [(mbpt: MyEnum) UsePrimaryAddress]; DataItem = 'MyUserControlViewModel' (HashCode = 21018822); Zielelement ist 'CheckEdit' (Name = ''); Zieleigenschaft ist 'EditValue' (Typ 'Objekt') – user1714232

+0

Äh, macht nichts. Ich habe einen Fehler im Bindungsweg gemacht. Ihre Lösung funktioniert jetzt. Vielen Dank :) – user1714232

Verwandte Themen