开发者

ListBoxItem with value?

I would like you to ask if it is somehow possible to p开发者_开发问答rovide 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


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}}" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜