INotifyPropertyChanged not updating UI in DataTemplate, XAML, WPF











up vote
0
down vote

favorite












I want to display multiple rectangles with some properties, which I store in an Array of Arrays. The XAML for displaying looks like this:



<UserControl x:Class="Project.Views.SomeView">

<UserControl.Resources>
<DataTemplate x:Key="ObjectRowTemplate">
<Rectangle Fill="{Binding Path=color, UpdateSourceTrigger=PropertyChanged}" Height="15" Width="30" StrokeThickness="1px" Stroke="Blue" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<catel:EventToCommand DisableAssociatedObjectOnCannotExecute="False">
<catel:EventToCommand.Command>
<Binding RelativeSource="{RelativeSource AncestorType={x:Type Canvas}}" Path="DataContext.ObjectClickedCommand" />
</catel:EventToCommand.Command>

<catel:EventToCommand.CommandParameter>
<Binding Path="." />
</catel:EventToCommand.CommandParameter>
</catel:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>
</DataTemplate>

<DataTemplate x:Key="ObjectColumnTemplate">
<ItemsControl ItemsSource="{Binding ., UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{DynamicResource ObjectRowTemplate}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DataTemplate>

</UserControl.Resources>
<StackPanel>
<Canvas Background="Black" Width="{Binding CanvasWidth}" Height="600">
<Grid>
<ItemsControl ItemsSource="{Binding Path=BindedProperty, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{DynamicResource ObjectColumnTemplate}"/>
</Grid>
</Canvas>
</StackPanel>




there is a StackPanel with a canvas inside which shows the array of array via the two defined DataTemplates, by creating a Rectangle for each "slot" in the Array.



namespace ViewModels
{
public class ViewModel : AdvancedViewModelBase
{
public CreateGameFieldViewModel()
{
ObjectClickedCommand = new Command<Object>(ObjectClicked);

Objects = new Objects[15];
for (int i = 0; i < 15; i++)
{
Objects[i] = new Object[15];
for (int j = 0; j < 15; j++)
{
Objects[i][j] = new Object(someparams);
}
}
}

private Object _selectedObject;

private Object _object;

public Object Objects
{
get { return _object; }
set
{
_object = value;
RaisePropertyChanged(nameof(Objects));
}
}

public int CanvasWidth { get; set; } = 450;

public Command<OPbject> ObjectClickedCommand { get; set; }

public void ObjectClicked(Object object)
{
if (object != null)
{
_selectedObject = object;
}
}

public void OnKeyDown(KeyEventArgs e)
{
// Modify _currentObject
_currentObject.color = Brushes.Blue;

RaisePropertyChanged(nameof(Objects));
}
}
}


The property binding with the viewmodel works fine, the properties are set as defined. By clicking on a Rectangle, the object which is bound to, is now set as _selectedObject and can be modified by the arrow keys, which also works like a charm.



Since I modified one object of the array of arrays, which I am binding to the DataTemplate, I have to notify my UI which I am doing by then line RaisePropertyChanged(nameof(Objects)); (used Nuget: Catel) after the modification.



Unfortunately the UI does not get updated. I checked everything, the object in the array of arrays contains the modifications.



I guess it doesn't work because of the DataTemplates I used.



Can anyone help me?










share|improve this question




















  • 1




    Unless you don't add or remove objects from Objects, it should be sufficient that the array element implements INotifyPropertyChanged and fires the PropertyChanged event for all relevant properties. As a note, setting Mode=OneWay and UpdateSourceTrigger=PropertyChanged on the ItemsSource Binding is pointless.
    – Clemens
    Nov 8 at 17:16






  • 1




    Did you try managing this with an ObservableCollection of the changed objects? Sometimes an object inside a collection never gets the event raised to the UI if it is bound to the collection itself.
    – SASS_Shooter
    Nov 8 at 22:29















up vote
0
down vote

favorite












I want to display multiple rectangles with some properties, which I store in an Array of Arrays. The XAML for displaying looks like this:



<UserControl x:Class="Project.Views.SomeView">

<UserControl.Resources>
<DataTemplate x:Key="ObjectRowTemplate">
<Rectangle Fill="{Binding Path=color, UpdateSourceTrigger=PropertyChanged}" Height="15" Width="30" StrokeThickness="1px" Stroke="Blue" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<catel:EventToCommand DisableAssociatedObjectOnCannotExecute="False">
<catel:EventToCommand.Command>
<Binding RelativeSource="{RelativeSource AncestorType={x:Type Canvas}}" Path="DataContext.ObjectClickedCommand" />
</catel:EventToCommand.Command>

<catel:EventToCommand.CommandParameter>
<Binding Path="." />
</catel:EventToCommand.CommandParameter>
</catel:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>
</DataTemplate>

<DataTemplate x:Key="ObjectColumnTemplate">
<ItemsControl ItemsSource="{Binding ., UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{DynamicResource ObjectRowTemplate}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DataTemplate>

</UserControl.Resources>
<StackPanel>
<Canvas Background="Black" Width="{Binding CanvasWidth}" Height="600">
<Grid>
<ItemsControl ItemsSource="{Binding Path=BindedProperty, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{DynamicResource ObjectColumnTemplate}"/>
</Grid>
</Canvas>
</StackPanel>




there is a StackPanel with a canvas inside which shows the array of array via the two defined DataTemplates, by creating a Rectangle for each "slot" in the Array.



namespace ViewModels
{
public class ViewModel : AdvancedViewModelBase
{
public CreateGameFieldViewModel()
{
ObjectClickedCommand = new Command<Object>(ObjectClicked);

Objects = new Objects[15];
for (int i = 0; i < 15; i++)
{
Objects[i] = new Object[15];
for (int j = 0; j < 15; j++)
{
Objects[i][j] = new Object(someparams);
}
}
}

private Object _selectedObject;

private Object _object;

public Object Objects
{
get { return _object; }
set
{
_object = value;
RaisePropertyChanged(nameof(Objects));
}
}

public int CanvasWidth { get; set; } = 450;

public Command<OPbject> ObjectClickedCommand { get; set; }

public void ObjectClicked(Object object)
{
if (object != null)
{
_selectedObject = object;
}
}

public void OnKeyDown(KeyEventArgs e)
{
// Modify _currentObject
_currentObject.color = Brushes.Blue;

RaisePropertyChanged(nameof(Objects));
}
}
}


The property binding with the viewmodel works fine, the properties are set as defined. By clicking on a Rectangle, the object which is bound to, is now set as _selectedObject and can be modified by the arrow keys, which also works like a charm.



Since I modified one object of the array of arrays, which I am binding to the DataTemplate, I have to notify my UI which I am doing by then line RaisePropertyChanged(nameof(Objects)); (used Nuget: Catel) after the modification.



Unfortunately the UI does not get updated. I checked everything, the object in the array of arrays contains the modifications.



I guess it doesn't work because of the DataTemplates I used.



Can anyone help me?










share|improve this question




















  • 1




    Unless you don't add or remove objects from Objects, it should be sufficient that the array element implements INotifyPropertyChanged and fires the PropertyChanged event for all relevant properties. As a note, setting Mode=OneWay and UpdateSourceTrigger=PropertyChanged on the ItemsSource Binding is pointless.
    – Clemens
    Nov 8 at 17:16






  • 1




    Did you try managing this with an ObservableCollection of the changed objects? Sometimes an object inside a collection never gets the event raised to the UI if it is bound to the collection itself.
    – SASS_Shooter
    Nov 8 at 22:29













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I want to display multiple rectangles with some properties, which I store in an Array of Arrays. The XAML for displaying looks like this:



<UserControl x:Class="Project.Views.SomeView">

<UserControl.Resources>
<DataTemplate x:Key="ObjectRowTemplate">
<Rectangle Fill="{Binding Path=color, UpdateSourceTrigger=PropertyChanged}" Height="15" Width="30" StrokeThickness="1px" Stroke="Blue" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<catel:EventToCommand DisableAssociatedObjectOnCannotExecute="False">
<catel:EventToCommand.Command>
<Binding RelativeSource="{RelativeSource AncestorType={x:Type Canvas}}" Path="DataContext.ObjectClickedCommand" />
</catel:EventToCommand.Command>

<catel:EventToCommand.CommandParameter>
<Binding Path="." />
</catel:EventToCommand.CommandParameter>
</catel:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>
</DataTemplate>

<DataTemplate x:Key="ObjectColumnTemplate">
<ItemsControl ItemsSource="{Binding ., UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{DynamicResource ObjectRowTemplate}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DataTemplate>

</UserControl.Resources>
<StackPanel>
<Canvas Background="Black" Width="{Binding CanvasWidth}" Height="600">
<Grid>
<ItemsControl ItemsSource="{Binding Path=BindedProperty, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{DynamicResource ObjectColumnTemplate}"/>
</Grid>
</Canvas>
</StackPanel>




there is a StackPanel with a canvas inside which shows the array of array via the two defined DataTemplates, by creating a Rectangle for each "slot" in the Array.



namespace ViewModels
{
public class ViewModel : AdvancedViewModelBase
{
public CreateGameFieldViewModel()
{
ObjectClickedCommand = new Command<Object>(ObjectClicked);

Objects = new Objects[15];
for (int i = 0; i < 15; i++)
{
Objects[i] = new Object[15];
for (int j = 0; j < 15; j++)
{
Objects[i][j] = new Object(someparams);
}
}
}

private Object _selectedObject;

private Object _object;

public Object Objects
{
get { return _object; }
set
{
_object = value;
RaisePropertyChanged(nameof(Objects));
}
}

public int CanvasWidth { get; set; } = 450;

public Command<OPbject> ObjectClickedCommand { get; set; }

public void ObjectClicked(Object object)
{
if (object != null)
{
_selectedObject = object;
}
}

public void OnKeyDown(KeyEventArgs e)
{
// Modify _currentObject
_currentObject.color = Brushes.Blue;

RaisePropertyChanged(nameof(Objects));
}
}
}


The property binding with the viewmodel works fine, the properties are set as defined. By clicking on a Rectangle, the object which is bound to, is now set as _selectedObject and can be modified by the arrow keys, which also works like a charm.



Since I modified one object of the array of arrays, which I am binding to the DataTemplate, I have to notify my UI which I am doing by then line RaisePropertyChanged(nameof(Objects)); (used Nuget: Catel) after the modification.



Unfortunately the UI does not get updated. I checked everything, the object in the array of arrays contains the modifications.



I guess it doesn't work because of the DataTemplates I used.



Can anyone help me?










share|improve this question















I want to display multiple rectangles with some properties, which I store in an Array of Arrays. The XAML for displaying looks like this:



<UserControl x:Class="Project.Views.SomeView">

<UserControl.Resources>
<DataTemplate x:Key="ObjectRowTemplate">
<Rectangle Fill="{Binding Path=color, UpdateSourceTrigger=PropertyChanged}" Height="15" Width="30" StrokeThickness="1px" Stroke="Blue" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<catel:EventToCommand DisableAssociatedObjectOnCannotExecute="False">
<catel:EventToCommand.Command>
<Binding RelativeSource="{RelativeSource AncestorType={x:Type Canvas}}" Path="DataContext.ObjectClickedCommand" />
</catel:EventToCommand.Command>

<catel:EventToCommand.CommandParameter>
<Binding Path="." />
</catel:EventToCommand.CommandParameter>
</catel:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
</Rectangle>
</DataTemplate>

<DataTemplate x:Key="ObjectColumnTemplate">
<ItemsControl ItemsSource="{Binding ., UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{DynamicResource ObjectRowTemplate}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DataTemplate>

</UserControl.Resources>
<StackPanel>
<Canvas Background="Black" Width="{Binding CanvasWidth}" Height="600">
<Grid>
<ItemsControl ItemsSource="{Binding Path=BindedProperty, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{DynamicResource ObjectColumnTemplate}"/>
</Grid>
</Canvas>
</StackPanel>




there is a StackPanel with a canvas inside which shows the array of array via the two defined DataTemplates, by creating a Rectangle for each "slot" in the Array.



namespace ViewModels
{
public class ViewModel : AdvancedViewModelBase
{
public CreateGameFieldViewModel()
{
ObjectClickedCommand = new Command<Object>(ObjectClicked);

Objects = new Objects[15];
for (int i = 0; i < 15; i++)
{
Objects[i] = new Object[15];
for (int j = 0; j < 15; j++)
{
Objects[i][j] = new Object(someparams);
}
}
}

private Object _selectedObject;

private Object _object;

public Object Objects
{
get { return _object; }
set
{
_object = value;
RaisePropertyChanged(nameof(Objects));
}
}

public int CanvasWidth { get; set; } = 450;

public Command<OPbject> ObjectClickedCommand { get; set; }

public void ObjectClicked(Object object)
{
if (object != null)
{
_selectedObject = object;
}
}

public void OnKeyDown(KeyEventArgs e)
{
// Modify _currentObject
_currentObject.color = Brushes.Blue;

RaisePropertyChanged(nameof(Objects));
}
}
}


The property binding with the viewmodel works fine, the properties are set as defined. By clicking on a Rectangle, the object which is bound to, is now set as _selectedObject and can be modified by the arrow keys, which also works like a charm.



Since I modified one object of the array of arrays, which I am binding to the DataTemplate, I have to notify my UI which I am doing by then line RaisePropertyChanged(nameof(Objects)); (used Nuget: Catel) after the modification.



Unfortunately the UI does not get updated. I checked everything, the object in the array of arrays contains the modifications.



I guess it doesn't work because of the DataTemplates I used.



Can anyone help me?







c# wpf xaml mvvm inotifypropertychanged






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 5:20









Blacktempel

2,85231639




2,85231639










asked Nov 8 at 16:40









EliaZ

1




1








  • 1




    Unless you don't add or remove objects from Objects, it should be sufficient that the array element implements INotifyPropertyChanged and fires the PropertyChanged event for all relevant properties. As a note, setting Mode=OneWay and UpdateSourceTrigger=PropertyChanged on the ItemsSource Binding is pointless.
    – Clemens
    Nov 8 at 17:16






  • 1




    Did you try managing this with an ObservableCollection of the changed objects? Sometimes an object inside a collection never gets the event raised to the UI if it is bound to the collection itself.
    – SASS_Shooter
    Nov 8 at 22:29














  • 1




    Unless you don't add or remove objects from Objects, it should be sufficient that the array element implements INotifyPropertyChanged and fires the PropertyChanged event for all relevant properties. As a note, setting Mode=OneWay and UpdateSourceTrigger=PropertyChanged on the ItemsSource Binding is pointless.
    – Clemens
    Nov 8 at 17:16






  • 1




    Did you try managing this with an ObservableCollection of the changed objects? Sometimes an object inside a collection never gets the event raised to the UI if it is bound to the collection itself.
    – SASS_Shooter
    Nov 8 at 22:29








1




1




Unless you don't add or remove objects from Objects, it should be sufficient that the array element implements INotifyPropertyChanged and fires the PropertyChanged event for all relevant properties. As a note, setting Mode=OneWay and UpdateSourceTrigger=PropertyChanged on the ItemsSource Binding is pointless.
– Clemens
Nov 8 at 17:16




Unless you don't add or remove objects from Objects, it should be sufficient that the array element implements INotifyPropertyChanged and fires the PropertyChanged event for all relevant properties. As a note, setting Mode=OneWay and UpdateSourceTrigger=PropertyChanged on the ItemsSource Binding is pointless.
– Clemens
Nov 8 at 17:16




1




1




Did you try managing this with an ObservableCollection of the changed objects? Sometimes an object inside a collection never gets the event raised to the UI if it is bound to the collection itself.
– SASS_Shooter
Nov 8 at 22:29




Did you try managing this with an ObservableCollection of the changed objects? Sometimes an object inside a collection never gets the event raised to the UI if it is bound to the collection itself.
– SASS_Shooter
Nov 8 at 22:29

















active

oldest

votes











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',
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
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212282%2finotifypropertychanged-not-updating-ui-in-datatemplate-xaml-wpf%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212282%2finotifypropertychanged-not-updating-ui-in-datatemplate-xaml-wpf%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)