TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Dietmar Brueckmann
1.7k
66
1.2k
Build a CheckListBox
Jul 10 2019 6:50 AM
Hello,
I've tried to build a usercontrol CheckListBox similar to
https://www.codeproject.com/Tips/1210187/%2FTips%2F1210187%2FA-WPF-CheckBox-ListBox
But this example doesn't show how to bind IsChecked or IsSelected to Viewmodels data.
In the xaml is the line IsChecked="{TemplateBinding IsSelected}"
I've build an observable list based on simple class "Satz" with IsChecked-property.
The app works, but my IsChecked-property Is'nt used, because I don't know how to connect it to "TemplateBinding IsSelected"
This is the esentiell Part of the xaml:
<
ListBox
Margin
=
"15"
VerticalAlignment
=
"Stretch"
ItemsSource
=
"{Binding Items}"
SelectionMode
=
"Multiple"
>
<
ListBox.Resources
>
<
Style
TargetType
=
"ListBoxItem"
>
<
Setter
Property
=
"OverridesDefaultStyle"
Value
=
"true"
/>
<
Setter
Property
=
"SnapsToDevicePixels"
Value
=
"true"
/>
<
Setter
Property
=
"Template"
>
<
Setter.Value
>
<
ControlTemplate
TargetType
=
"ListBoxItem"
>
<
CheckBox
Margin
=
"5,2"
IsChecked
=
"{TemplateBinding IsSelected}"
>
<
ContentPresenter
/>
</
CheckBox
>
</
ControlTemplate
>
</
Setter.Value
>
</
Setter
>
</
Style
>
</
ListBox.Resources
>
</
ListBox
>
and this the part of ViewModel.cs
public
enum
EnumCheckListBox
{
hare, hedgehog, deer, frog, fox, wolf, wild_boar
}
public
class
Satz
{
public
EnumCheckListBox What {
get
;
set
; }
public
override
string
ToString()
{
return
What.ToString();
}
public
bool
IsChecked {
get
;
set
; }
}
public
class
ViewModel
{
public
ObservableCollection<Satz> Items {
get
; } =
new
ObservableCollection<Satz>();
public
ViewModel()
{
foreach
(EnumCheckListBox enm
in
Enum.GetValues(
typeof
(EnumCheckListBox)))
{
Items.Add(
new
Satz {
What = enm,
IsChecked = enm == EnumCheckListBox.deer
});
}
}
}
best regards Dietmar
Attachment:
TestCheckListBox.zip
Reply
Answers (
1
)
my wpf application crashing
Wpf Application freeze issue