且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

枚举到Wpf ListBoxItem作为CheckBox

更新时间:2022-06-27 02:40:50

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!