2013-03-05 4 views
4

키를 열거 형으로 사용하고 값을 사용자 지정 개체로 사용하는 사용자 지정 사전이 있습니다. 이 객체를 xaml에 바인딩해야합니다. 그래서 내가 어떻게 할 수 있니? 내가하고 싶은 것은XAML에서 enum 값에 액세스하는 방법

,

내가 시도 것을
<Button Content="{Binding ButtonGroups[my enum value].Text}"></Button> 

,

<Button Content="{Binding ButtonGroups[local:MyEnum.Report].Text}"></Button> 

<Button Content="{Binding ButtonGroups[x:Static local:MyEnum.Report].Text}"> 
</Button> 

<Button Content="{Binding ButtonGroups[{x:Static local:MyEnum.Report}].Text}"> 
</Button> 

그러나 위의 코드는 열거 값을 표시하고 다음은 내꺼 위해 일하지 않고, 열거 파일

<Button Content="{x:Static local:MyEnum.Report}"></Button> 

,

,
public enum MyEnum 
{ 
    Home, 
    Report 
} 

내 사전,

IDictionary<MyEnum, Button> ButtonGroups 

답변

4

당신 만 Enum 값을 이용해야한다, 그러나 Text 속성을 Button이없는, 그래서 Content

<Button Content="{Binding ButtonGroups[Home].Content}"> 

시험 예 사용 :

Xaml :

<Window x:Class="WpfApplication13.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" x:Name="UI" Width="294" Height="79" > 

    <Grid DataContext="{Binding ElementName=UI}"> 
     <Button Content="{Binding ButtonGroups[Home].Content}" /> 
    </Grid> 
</Window> 

코드 :

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     ButtonGroups.Add(MyEnum.Home, new Button { Content = "Hello" }); 
     NotifyPropertyChanged("ButtonGroups"); 
    } 

    private Dictionary<MyEnum, Button> _buttonGroups = new Dictionary<MyEnum, Button>(); 
    public Dictionary<MyEnum, Button> ButtonGroups 
    { 
     get { return _buttonGroups; } 
     set { _buttonGroups = value; } 
    } 

    public enum MyEnum 
    { 
     Home, 
     Report 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(string property) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(property)); 
     } 
    } 
} 

결과 : 나는 {경로 = ButtonGroups [(현지 : MyEnum) 홈] 바인딩 "=`에 의해 <버튼 콘텐츠 작업을 얻었다

enter image description here

+0

는 .text } "/>',하지만 훨씬 쉽습니다 :) 그리고'Text'는 버튼의 속성이 아닙니다. 해당 컬렉션에는 사용자 지정 개체가 들어 있으며 해당 사용자 지정 개체에는 'Text'라는 속성이 있습니다. 빠른 답장과 답변을 주셔서 감사합니다 –

+0

문제 없음 :) 해피 코딩 –

관련 문제