2012-10-02 12 views
5

मैं कुछ एप्लिकेशन हूं और मैं कुछ टेक्स्टबॉक्स और चेकबॉक्स को शब्दकोश के मूल्य क्षेत्र (एनम, स्ट्रिंग) से जोड़ना चाहता हूं। क्या यह संभव है और मैं यह कैसे कर सकता हूं?एनयू के साथ शब्दकोश में मूल्य के लिए बाध्यकारी

XAML कोड में मैं कुछ इस तरह है - यह एक कुंजी के रूप में तार के साथ शब्दकोश के लिए काम कर रहा है, लेकिन इसे सही ढंग से 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}" /> 

साथ कुंजी करने के लिए बाध्य नहीं कर सकते .. और यहाँ है कि मैं क्या Enum में है

public Dictionary<MyEnum, string> Properties 

मैं solut पाया है: के रूप में

public enum MyEnum 
{ 
    PrimaryAddress, 
    SecondaryAddress, 
    UsePrimaryAddress 
} 

ViewModel शब्दकोश में परिभाषित किया गया है enum मूल्यों के साथ combobox के लिए आयन लेकिन यह मेरे मामले पर लागू नहीं होता है।

कोई सलाह?

उत्तर

9

आपको बाइंडिंग अभिव्यक्ति में अनुक्रमणिका के पैरामीटर के लिए उपयुक्त प्रकार सेट करना होगा।

देखें मॉडल:

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> 

कोड-पीछे:

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

अधिक जानकारी के लिए, "Binding Path Syntax" देखें।

+0

उपरोक्त बाध्यकारी पथ का उपयोग करके मुझे निम्न त्रुटि मिली: System.Windows.Data त्रुटि: 40: बाइंडिंग एक्सप्रेशन पथ त्रुटि: '[]' संपत्ति 'ऑब्जेक्ट' '' शब्दकोश '2 '(हैशकोड = 56465364) पर नहीं मिली है। BindingExpression: पथ = गुण [(MbPT: MyEnum) UsePrimaryAddress]; DataItem = 'MyUserControlViewModel' (हैशकोड = 21018822); लक्ष्य तत्व 'चेकएडिट' (नाम = '') है; लक्षित संपत्ति 'EditValue' है (प्रकार 'ऑब्जेक्ट') – user1714232

+0

उह, कभी भी ध्यान न दें। मैंने बाध्यकारी पथ में कुछ गलती की। आपका समाधान अब काम कर रहा है। धन्यवाद :) – user1714232

संबंधित मुद्दे