枚举到Wpf ListBoxItem作为CheckBox [英] Enum to Wpf ListBoxItem as CheckBox

查看:64
本文介绍了枚举到Wpf ListBoxItem作为CheckBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有一个枚举,我希望每个枚举项都有一个复选框,
在每个listboxitem(看起来像一张桌子)中

我尝试绘制" ListBox的一行:
("x"代替CheckBox)

--------------------
| X X X(一些文本)|
--------------------

因为只有3个EnumItems我做了硬编码",
对于每个枚举都有一个属性,但是如果我添加一个...:〜

任何对我的提示都会很棒!


Hello there,


I have an enum and i want for every enum item a checkbox,
in every listboxitem (looks like a table)

i try to ''draw'' one line of the ListBox:
(the ''x'' is instead of a CheckBox)

--------------------
|X X X (some text) |
--------------------

Because there are just 3 EnumItems i did it ''hardcoded'',
for every enum a property, but if i add one ... :~

any hints for me would be great!


thanks in advance!

推荐答案

0)您可以在数据项中创建一个新属性,该属性将项目文本与枚举名称结合在一起,并将该新属性绑定到列表框项目.

1)您可以创建一个自定义ListBoxItem模板,该模板包含用于列表框项目文本和枚举名称的元素,然后将两个字段绑定到绑定数据项中的适当属性(枚举可能需要IValueConverter对象).
0) You could create a new property in your data item that combines the item text with the enum name, and bind that new property to the listbox item.

1) You could create a custom ListBoxItem template that contains an element for the listbox item text AND the enum name, and bind the two fields to appropriate properties in the bound data item (you may need an IValueConverter object for the enum).


不确定,如果我理解正确,那么我将xaml发布给您:
Im not sure, if i understand you right, so i post you my xaml:
<ListBox Name="listBox" Padding="3" SelectionMode="Extended"

         ItemsSource="{Binding Source={StaticResource ObservablCollectionList}}"

         SelectionChanged="listBox_SelectionChanged" MaxHeight="400">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <!-- TODO: extract enum ! -->
                <CheckBox IsChecked="{Binding ViewChecked}"/>
                <CheckBox IsChecked="{Binding InfoChecked}" />
                <CheckBox IsChecked="{Binding ProdChecked}"/>
                <TextBlock Text="{Binding Text}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>



和枚举:



And the enum:

/// <summary>
/// Mode
/// </summary>
public enum Modi
{
    /// <summary>
    /// View
    /// </summary>
    OhneStempel,
    /// <summary>
    /// Information
    /// </summary>
    InfoDruck,
    /// <summary>
    /// Production
    /// </summary>
    ProduktionsDruck
};




我想要这种样式,但我想说的是绑定到枚举的另一个列表,而不是3个复选框.很难解释您不了解的内容;)
但我希望你能理解我的解释...?

但无论如何,我对ValueConverters并不熟悉.也许这就是解决方案,但是我可以请您向我解释一下吗?

非常感谢!




i want this style, but instead of the 3 Checkboxes i want lets say another list bound to the enum. its hard to explain something you dont understand ;)
but i hope you understand my explanation...?

but anyway im not familiar with ValueConverters. Maybe thats the solution, but may i ask you to explain it with a little snippet to me?

thanks a lot!


这篇关于枚举到Wpf ListBoxItem作为CheckBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆