views:

49

answers:

1

Hi ! I would like you to ask if it is somehow possible to provide in the ListBoxItem the string that will appear and the value to be stored in DB. This is indeed possible:

ItemSource={Binding MyEnumColleciton}

or

ItemSource={DynamicResource MyCollection}

etc..

but if you image that I have about 100 ListBoxes .. I don't want to have so many different enumerations and other ItemSource collections, I want to write it directly into the ListBoxItem.

This is what I'm talking about:

...
<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
A: 

Well I came up with this:

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; }
    }

Add a DP into the control's resources:

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

And then:

<ListBox SelectedValue="{Binding Path=A}" SelectedValuePath="Value" Style="{StaticResource RadioButtonList}" DisplayMemberPath="Text" ItemsSource="{Binding Source={StaticResource testODP}}" />
PaN1C_Showt1Me
It's not directly what I wanted.. but.. Your opinions ?
PaN1C_Showt1Me