2010-08-05 64 views
1

我想問你是否可以在ListBoxItem提供的字符串中出現的值存儲在DB中。 這的確是可能的:帶有值的ListBoxItem?

ItemSource={Binding MyEnumColleciton} 

ItemSource={DynamicResource MyCollection} 

等。

,但如果你像我有大約100列表框..我不希望有這麼多不同的枚舉和其他ItemSource集合,我想直接寫入ListBoxItem。

這就是我說的:

<ListBox SelectedItem="{Binding Path=MyPath1}" Style="{StaticResource RadioButtonList}"> 
    <ListBoxItem Content="Text1" /> 
    <ListBoxItem Content="Text2" /> 
</ListBox> 

<ListBox SelectedItem="{Binding Path=MyPath2}" Style="{StaticResource RadioButtonList}"> 
    <ListBoxItem Content="Text3" /> 
    <ListBoxItem Content="Text4" /> 
</ListBox> 

<ListBox SelectedItem="{Binding Path=MyPath3}" Style="{StaticResource RadioButtonList}"> 
    <ListBoxItem Content="Text5" /> 
    <ListBoxItem Content="Text6" /> 
</ListBox> 

... 100x 

回答

0

好吧,我想出了這個:

public class ItemSourceProvider 
    { 
     public IEnumerable<ValueText<int>> GetValues(object o) 
     { 
      if (o == null) return null; 

      switch (o.ToString().ToUpper()) 
      { 
       case "PARAM": 
       { 
        return new List<ValueText<int>>() 
        { 
         new ValueText<int>{Value = 1, Text = "YES"}, 
         new ValueText<int>{Value = 2, Text = "PARTIALLY"}, 
         new ValueText<int>{Value = 3, Text = "NO"} 
        }; 
       } 
       default: return null; 
      } 
     } 
    } 

    public class ValueText<T> 
    { 
     public string Text { get; set; } 
     public T Value { get; set; } 
    } 

添加一個DP到控制資源:

<ObjectDataProvider x:Key="testODP" MethodName="GetValues" ObjectType="{x:Type local:ItemSourceProvider}"> 
    <ObjectDataProvider.MethodParameters>PARAM</ObjectDataProvider.MethodParameters>        
</ObjectDataProvider> 

而且那麼:

<ListBox SelectedValue="{Binding Path=A}" SelectedValuePath="Value" Style="{StaticResource RadioButtonList}" DisplayMemberPath="Text" ItemsSource="{Binding Source={StaticResource testODP}}" /> 
+0

這不是直接我想要的..但​​是.. 您的意見? – 2010-08-05 14:09:02