listview not refreshed when using databinding
I have a model class, Book, which contains a Keywords property:
public class Book : INotifyPropertyChanged
private ObservableCollection<string> _keywords;
...
public ObservableCollection<string> Keywords
get => _keywords;
set
_keywords = value;
OnPropertyChanged("Keywords");
and in my MainPage I have 2 components : a list View and a combobox whose each entry is a checkBox:
<ComboBox
x:Name="cbb_Keywords"
Grid.Column="2"
Width="300"
Margin="5,0,0,0"
HorizontalAlignment="Left"
ItemsSource="Binding Source=StaticResource AllBooks"
DataContext="Binding ElementName=listBoxBooks,Path=SelectedItem,UpdateSourceTrigger=PropertyChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Width="200" Content="Binding" Click="ButtonBase_OnClick">
<CheckBox.IsChecked>
<MultiBinding Converter="StaticResource TextInListTrueFalseConverter" Mode="OneWay">
<Binding ElementName="listBoxBooks" Path="SelectedItem.KeywordsForTextbox" Mode="OneWay"></Binding>
<Binding RelativeSource="RelativeSource Self" Path="Content"></Binding>
</MultiBinding>
</CheckBox.IsChecked>
</CheckBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
the checkBox.IsChecked multibinding is oneway, and when I click on a checkbox, it calls this method:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
CheckBox cb = (CheckBox)sender;
var content = (string)cb.Content;
var keywords = ((Book)listBoxBooks.SelectedItem).Keywords;
bool clicked = cb.IsChecked.Value;
if (clicked)
keywords.Add(content);
else
keywords.Remove(content);
it works more or less but there are 2 caveats:
sometimes the checkbox on which I just clicked is displayed in the combobox's checkbox, which is not expected and is annoying
I have, in addition of the combobox, an other component, a textbox, which contains the list of the keywords for the listview's selectedItem:
but when I click on a checkbox to toogle this, the listbox containing the list is not refreshed...
so I chenged a little my Keywords property, in Book:
public ObservableCollection<string> Keywords
get => _keywords;
set
_keywords = value;
OnPropertyChanged("Keywords");
OnPropertyChanged("KeywordsForTextbox");
and the KeywordsForTextbox property is like this:
public string KeywordsForTextbox
get return string.Join(",", _keywords);
finally, to be complete, here is the textBox component in my MainWindow:
<TextBox x:Name="txb_Keywords"
Grid.Column="1"
Width="500"
Text="Binding ElementName=listBoxBooks,Path=SelectedItem.KeywordsForTextbox,Mode=OneWay,UpdateSourceTrigger=PropertyChanged" />
why does the checkbox appears in the combobox's textbox? why isn't refreshed the other textbox?
thank you.
c# wpf data-binding
add a comment |
I have a model class, Book, which contains a Keywords property:
public class Book : INotifyPropertyChanged
private ObservableCollection<string> _keywords;
...
public ObservableCollection<string> Keywords
get => _keywords;
set
_keywords = value;
OnPropertyChanged("Keywords");
and in my MainPage I have 2 components : a list View and a combobox whose each entry is a checkBox:
<ComboBox
x:Name="cbb_Keywords"
Grid.Column="2"
Width="300"
Margin="5,0,0,0"
HorizontalAlignment="Left"
ItemsSource="Binding Source=StaticResource AllBooks"
DataContext="Binding ElementName=listBoxBooks,Path=SelectedItem,UpdateSourceTrigger=PropertyChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Width="200" Content="Binding" Click="ButtonBase_OnClick">
<CheckBox.IsChecked>
<MultiBinding Converter="StaticResource TextInListTrueFalseConverter" Mode="OneWay">
<Binding ElementName="listBoxBooks" Path="SelectedItem.KeywordsForTextbox" Mode="OneWay"></Binding>
<Binding RelativeSource="RelativeSource Self" Path="Content"></Binding>
</MultiBinding>
</CheckBox.IsChecked>
</CheckBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
the checkBox.IsChecked multibinding is oneway, and when I click on a checkbox, it calls this method:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
CheckBox cb = (CheckBox)sender;
var content = (string)cb.Content;
var keywords = ((Book)listBoxBooks.SelectedItem).Keywords;
bool clicked = cb.IsChecked.Value;
if (clicked)
keywords.Add(content);
else
keywords.Remove(content);
it works more or less but there are 2 caveats:
sometimes the checkbox on which I just clicked is displayed in the combobox's checkbox, which is not expected and is annoying
I have, in addition of the combobox, an other component, a textbox, which contains the list of the keywords for the listview's selectedItem:
but when I click on a checkbox to toogle this, the listbox containing the list is not refreshed...
so I chenged a little my Keywords property, in Book:
public ObservableCollection<string> Keywords
get => _keywords;
set
_keywords = value;
OnPropertyChanged("Keywords");
OnPropertyChanged("KeywordsForTextbox");
and the KeywordsForTextbox property is like this:
public string KeywordsForTextbox
get return string.Join(",", _keywords);
finally, to be complete, here is the textBox component in my MainWindow:
<TextBox x:Name="txb_Keywords"
Grid.Column="1"
Width="500"
Text="Binding ElementName=listBoxBooks,Path=SelectedItem.KeywordsForTextbox,Mode=OneWay,UpdateSourceTrigger=PropertyChanged" />
why does the checkbox appears in the combobox's textbox? why isn't refreshed the other textbox?
thank you.
c# wpf data-binding
add a comment |
I have a model class, Book, which contains a Keywords property:
public class Book : INotifyPropertyChanged
private ObservableCollection<string> _keywords;
...
public ObservableCollection<string> Keywords
get => _keywords;
set
_keywords = value;
OnPropertyChanged("Keywords");
and in my MainPage I have 2 components : a list View and a combobox whose each entry is a checkBox:
<ComboBox
x:Name="cbb_Keywords"
Grid.Column="2"
Width="300"
Margin="5,0,0,0"
HorizontalAlignment="Left"
ItemsSource="Binding Source=StaticResource AllBooks"
DataContext="Binding ElementName=listBoxBooks,Path=SelectedItem,UpdateSourceTrigger=PropertyChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Width="200" Content="Binding" Click="ButtonBase_OnClick">
<CheckBox.IsChecked>
<MultiBinding Converter="StaticResource TextInListTrueFalseConverter" Mode="OneWay">
<Binding ElementName="listBoxBooks" Path="SelectedItem.KeywordsForTextbox" Mode="OneWay"></Binding>
<Binding RelativeSource="RelativeSource Self" Path="Content"></Binding>
</MultiBinding>
</CheckBox.IsChecked>
</CheckBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
the checkBox.IsChecked multibinding is oneway, and when I click on a checkbox, it calls this method:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
CheckBox cb = (CheckBox)sender;
var content = (string)cb.Content;
var keywords = ((Book)listBoxBooks.SelectedItem).Keywords;
bool clicked = cb.IsChecked.Value;
if (clicked)
keywords.Add(content);
else
keywords.Remove(content);
it works more or less but there are 2 caveats:
sometimes the checkbox on which I just clicked is displayed in the combobox's checkbox, which is not expected and is annoying
I have, in addition of the combobox, an other component, a textbox, which contains the list of the keywords for the listview's selectedItem:
but when I click on a checkbox to toogle this, the listbox containing the list is not refreshed...
so I chenged a little my Keywords property, in Book:
public ObservableCollection<string> Keywords
get => _keywords;
set
_keywords = value;
OnPropertyChanged("Keywords");
OnPropertyChanged("KeywordsForTextbox");
and the KeywordsForTextbox property is like this:
public string KeywordsForTextbox
get return string.Join(",", _keywords);
finally, to be complete, here is the textBox component in my MainWindow:
<TextBox x:Name="txb_Keywords"
Grid.Column="1"
Width="500"
Text="Binding ElementName=listBoxBooks,Path=SelectedItem.KeywordsForTextbox,Mode=OneWay,UpdateSourceTrigger=PropertyChanged" />
why does the checkbox appears in the combobox's textbox? why isn't refreshed the other textbox?
thank you.
c# wpf data-binding
I have a model class, Book, which contains a Keywords property:
public class Book : INotifyPropertyChanged
private ObservableCollection<string> _keywords;
...
public ObservableCollection<string> Keywords
get => _keywords;
set
_keywords = value;
OnPropertyChanged("Keywords");
and in my MainPage I have 2 components : a list View and a combobox whose each entry is a checkBox:
<ComboBox
x:Name="cbb_Keywords"
Grid.Column="2"
Width="300"
Margin="5,0,0,0"
HorizontalAlignment="Left"
ItemsSource="Binding Source=StaticResource AllBooks"
DataContext="Binding ElementName=listBoxBooks,Path=SelectedItem,UpdateSourceTrigger=PropertyChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Width="200" Content="Binding" Click="ButtonBase_OnClick">
<CheckBox.IsChecked>
<MultiBinding Converter="StaticResource TextInListTrueFalseConverter" Mode="OneWay">
<Binding ElementName="listBoxBooks" Path="SelectedItem.KeywordsForTextbox" Mode="OneWay"></Binding>
<Binding RelativeSource="RelativeSource Self" Path="Content"></Binding>
</MultiBinding>
</CheckBox.IsChecked>
</CheckBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
the checkBox.IsChecked multibinding is oneway, and when I click on a checkbox, it calls this method:
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
CheckBox cb = (CheckBox)sender;
var content = (string)cb.Content;
var keywords = ((Book)listBoxBooks.SelectedItem).Keywords;
bool clicked = cb.IsChecked.Value;
if (clicked)
keywords.Add(content);
else
keywords.Remove(content);
it works more or less but there are 2 caveats:
sometimes the checkbox on which I just clicked is displayed in the combobox's checkbox, which is not expected and is annoying
I have, in addition of the combobox, an other component, a textbox, which contains the list of the keywords for the listview's selectedItem:
but when I click on a checkbox to toogle this, the listbox containing the list is not refreshed...
so I chenged a little my Keywords property, in Book:
public ObservableCollection<string> Keywords
get => _keywords;
set
_keywords = value;
OnPropertyChanged("Keywords");
OnPropertyChanged("KeywordsForTextbox");
and the KeywordsForTextbox property is like this:
public string KeywordsForTextbox
get return string.Join(",", _keywords);
finally, to be complete, here is the textBox component in my MainWindow:
<TextBox x:Name="txb_Keywords"
Grid.Column="1"
Width="500"
Text="Binding ElementName=listBoxBooks,Path=SelectedItem.KeywordsForTextbox,Mode=OneWay,UpdateSourceTrigger=PropertyChanged" />
why does the checkbox appears in the combobox's textbox? why isn't refreshed the other textbox?
thank you.
c# wpf data-binding
c# wpf data-binding
edited Nov 13 at 8:52
Rob
1,0091022
1,0091022
asked Nov 12 at 15:16
lolveley
71611022
71611022
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
The problem is that when modifying the Keywords collection the actual Keywords property doesn't change. It's still the same collection object. Only the object's properties (Items) change.
In your Book class you could use methods to do the adding, and removing, then notify property changed from there.
public void AddKeyword(string name)
Keywords.Add(name);
OnPropertyChanged("Keywords");
public void RemoveKeyword(string name)
Keywords.Remove(name);
OnPropertyChanged("Keywords");
Then change your event like this.
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
CheckBox cb = (CheckBox)sender;
var content = (string)cb.Content;
var book = ((Book)listBoxBooks.SelectedItem);
bool clicked = cb.IsChecked.Value;
if (clicked)
book.AddKeyword(content);
else
book.RemoveKeyword(content);
It works and I understand why it works, but I still don't understand why it did not work before, after all the purpose of an ObservableCollection is to raise an event when one of its element changes...which is the case.
– lolveley
Nov 12 at 18:19
@lolveley I believe that an ObservableCollection only notifies if the collection changes, i.e. an item is added or removed - I don't think it is watching for changes to the properties of the collection items. Unless the individual items are implementing INotifyPropertyChanged then the control is not aware - the collection items don't somehow inherit INotifyPropertyChanged because they're in an OC. At least, that's my understanding.
– Simon Evans
Nov 13 at 19:46
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265084%2flistview-not-refreshed-when-using-databinding%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The problem is that when modifying the Keywords collection the actual Keywords property doesn't change. It's still the same collection object. Only the object's properties (Items) change.
In your Book class you could use methods to do the adding, and removing, then notify property changed from there.
public void AddKeyword(string name)
Keywords.Add(name);
OnPropertyChanged("Keywords");
public void RemoveKeyword(string name)
Keywords.Remove(name);
OnPropertyChanged("Keywords");
Then change your event like this.
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
CheckBox cb = (CheckBox)sender;
var content = (string)cb.Content;
var book = ((Book)listBoxBooks.SelectedItem);
bool clicked = cb.IsChecked.Value;
if (clicked)
book.AddKeyword(content);
else
book.RemoveKeyword(content);
It works and I understand why it works, but I still don't understand why it did not work before, after all the purpose of an ObservableCollection is to raise an event when one of its element changes...which is the case.
– lolveley
Nov 12 at 18:19
@lolveley I believe that an ObservableCollection only notifies if the collection changes, i.e. an item is added or removed - I don't think it is watching for changes to the properties of the collection items. Unless the individual items are implementing INotifyPropertyChanged then the control is not aware - the collection items don't somehow inherit INotifyPropertyChanged because they're in an OC. At least, that's my understanding.
– Simon Evans
Nov 13 at 19:46
add a comment |
The problem is that when modifying the Keywords collection the actual Keywords property doesn't change. It's still the same collection object. Only the object's properties (Items) change.
In your Book class you could use methods to do the adding, and removing, then notify property changed from there.
public void AddKeyword(string name)
Keywords.Add(name);
OnPropertyChanged("Keywords");
public void RemoveKeyword(string name)
Keywords.Remove(name);
OnPropertyChanged("Keywords");
Then change your event like this.
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
CheckBox cb = (CheckBox)sender;
var content = (string)cb.Content;
var book = ((Book)listBoxBooks.SelectedItem);
bool clicked = cb.IsChecked.Value;
if (clicked)
book.AddKeyword(content);
else
book.RemoveKeyword(content);
It works and I understand why it works, but I still don't understand why it did not work before, after all the purpose of an ObservableCollection is to raise an event when one of its element changes...which is the case.
– lolveley
Nov 12 at 18:19
@lolveley I believe that an ObservableCollection only notifies if the collection changes, i.e. an item is added or removed - I don't think it is watching for changes to the properties of the collection items. Unless the individual items are implementing INotifyPropertyChanged then the control is not aware - the collection items don't somehow inherit INotifyPropertyChanged because they're in an OC. At least, that's my understanding.
– Simon Evans
Nov 13 at 19:46
add a comment |
The problem is that when modifying the Keywords collection the actual Keywords property doesn't change. It's still the same collection object. Only the object's properties (Items) change.
In your Book class you could use methods to do the adding, and removing, then notify property changed from there.
public void AddKeyword(string name)
Keywords.Add(name);
OnPropertyChanged("Keywords");
public void RemoveKeyword(string name)
Keywords.Remove(name);
OnPropertyChanged("Keywords");
Then change your event like this.
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
CheckBox cb = (CheckBox)sender;
var content = (string)cb.Content;
var book = ((Book)listBoxBooks.SelectedItem);
bool clicked = cb.IsChecked.Value;
if (clicked)
book.AddKeyword(content);
else
book.RemoveKeyword(content);
The problem is that when modifying the Keywords collection the actual Keywords property doesn't change. It's still the same collection object. Only the object's properties (Items) change.
In your Book class you could use methods to do the adding, and removing, then notify property changed from there.
public void AddKeyword(string name)
Keywords.Add(name);
OnPropertyChanged("Keywords");
public void RemoveKeyword(string name)
Keywords.Remove(name);
OnPropertyChanged("Keywords");
Then change your event like this.
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
CheckBox cb = (CheckBox)sender;
var content = (string)cb.Content;
var book = ((Book)listBoxBooks.SelectedItem);
bool clicked = cb.IsChecked.Value;
if (clicked)
book.AddKeyword(content);
else
book.RemoveKeyword(content);
edited Nov 12 at 15:49
answered Nov 12 at 15:43
Neil B
8691412
8691412
It works and I understand why it works, but I still don't understand why it did not work before, after all the purpose of an ObservableCollection is to raise an event when one of its element changes...which is the case.
– lolveley
Nov 12 at 18:19
@lolveley I believe that an ObservableCollection only notifies if the collection changes, i.e. an item is added or removed - I don't think it is watching for changes to the properties of the collection items. Unless the individual items are implementing INotifyPropertyChanged then the control is not aware - the collection items don't somehow inherit INotifyPropertyChanged because they're in an OC. At least, that's my understanding.
– Simon Evans
Nov 13 at 19:46
add a comment |
It works and I understand why it works, but I still don't understand why it did not work before, after all the purpose of an ObservableCollection is to raise an event when one of its element changes...which is the case.
– lolveley
Nov 12 at 18:19
@lolveley I believe that an ObservableCollection only notifies if the collection changes, i.e. an item is added or removed - I don't think it is watching for changes to the properties of the collection items. Unless the individual items are implementing INotifyPropertyChanged then the control is not aware - the collection items don't somehow inherit INotifyPropertyChanged because they're in an OC. At least, that's my understanding.
– Simon Evans
Nov 13 at 19:46
It works and I understand why it works, but I still don't understand why it did not work before, after all the purpose of an ObservableCollection is to raise an event when one of its element changes...which is the case.
– lolveley
Nov 12 at 18:19
It works and I understand why it works, but I still don't understand why it did not work before, after all the purpose of an ObservableCollection is to raise an event when one of its element changes...which is the case.
– lolveley
Nov 12 at 18:19
@lolveley I believe that an ObservableCollection only notifies if the collection changes, i.e. an item is added or removed - I don't think it is watching for changes to the properties of the collection items. Unless the individual items are implementing INotifyPropertyChanged then the control is not aware - the collection items don't somehow inherit INotifyPropertyChanged because they're in an OC. At least, that's my understanding.
– Simon Evans
Nov 13 at 19:46
@lolveley I believe that an ObservableCollection only notifies if the collection changes, i.e. an item is added or removed - I don't think it is watching for changes to the properties of the collection items. Unless the individual items are implementing INotifyPropertyChanged then the control is not aware - the collection items don't somehow inherit INotifyPropertyChanged because they're in an OC. At least, that's my understanding.
– Simon Evans
Nov 13 at 19:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265084%2flistview-not-refreshed-when-using-databinding%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown