wpf选中列表框问题
我正在使用以下模板创建选中列表框:
<Style x:Key="CheckBoxListStyle" TargetType="{x:Type ListBox}">
<Setter Property="SelectionMode" Value="Multiple"></Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<CheckBox IsChecked="{Binding Path=xxxxxxxxxxx,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"> <ContentPresenter></ContentPresenter>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
<ListBox Style="{StaticResource CheckBoxListStyle}" ItemsSource="{Binding Path=Configuration.ProductTypes}" DisplayMemberPath="ProductName" />
我的问题是,我从数据库获取所有产品类型并将其绑定到列表框项目源。我有一个产品类,其中包含它所属的产品类型列表。我需要根据产品的产品类型列表设置复选框的 IsChecked 属性。通常我需要设置一个或多个复选框 IsChecked 属性。产品可能属于一种或多种产品类型。选择方式为多选。如果用户选中其他产品类型复选框..我需要获取全部选中列表框并将它们保存到数据库...这是一个 MVVM WPF 应用程序...有关如何解决此场景的任何想法...< br> 在controltemplate中,IsChecked的路径是什么?
谢谢 雷伊
让我简单地解释一下我的问题:我需要一个带有复选框和文本块的列表框。文本块数据上下文不同,复选框数据上下文不同。我的意思是他们从不同的对象获取数据。不知道如何实现这一点......
I am creating checked list box with the following template:
<Style x:Key="CheckBoxListStyle" TargetType="{x:Type ListBox}">
<Setter Property="SelectionMode" Value="Multiple"></Setter>
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style TargetType="{x:Type ListBoxItem}" >
<Setter Property="Margin" Value="2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<CheckBox IsChecked="{Binding Path=xxxxxxxxxxx,RelativeSource={RelativeSource TemplatedParent},Mode=TwoWay}"> <ContentPresenter></ContentPresenter>
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
</Style>
<ListBox Style="{StaticResource CheckBoxListStyle}" ItemsSource="{Binding Path=Configuration.ProductTypes}" DisplayMemberPath="ProductName" />
my problem is, i am getting all the product types from Database and binding it to the list box itemssource. i have a product class which contains a list of product types it belongs to. i need to set the IsChecked property of check boxes based on Product's product type list. Typically i need to set one or more check boxes IsChecked property. product may belong to 1 or more product types. and selection mode is multiselect. if user checks the additional product type check boxes ..i need to get the total checked list boxes and save them to the database... this is a MVVM WPF application ... any ideas on how to solve this scenario...
in the controltemplate what will be the path is IsChecked?
Thanks
Rey
Let me simplyfy my question: I need a list box with checkbox and textblock. textblock data context is different and check box data context is different. i mean they get their data from different objects. no idea how to achieve this...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个新类(ViewModel),其中包含两个数据上下文(Model1 和 Model2)并将数据绑定到它,也称为 MVVM 模式。
Create a new class (a ViewModel) which contains the two data contexts (Model1 and Model2) and data bind to it, also known as the MVVM pattern.
绑定到 ListBox 的 ProductType 类应该有一个可以绑定的名为 IsSelected 或 IsChecked 的读/写属性。然后,不要使用 ControlTemplate,而是使用 ListBox.ItemTemplate 上的 DataTemplate 并将 CheckBox 的 IsChecked 属性绑定到该属性。
The ProductType class that you're binding to the ListBox should have a read/write property called IsSelected or IsChecked that you can bind to. Then instead of using a ControlTemplate, use a DataTemplate on the ListBox.ItemTemplate and bind the CheckBox's IsChecked property to that property.