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 DataTemplate
s, 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 DataTemplate
s I used.
Can anyone help me?
c# wpf xaml mvvm inotifypropertychanged
add a comment |
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 DataTemplate
s, 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 DataTemplate
s I used.
Can anyone help me?
c# wpf xaml mvvm inotifypropertychanged
1
Unless you don't add or remove objects fromObjects
, it should be sufficient that the array element implements INotifyPropertyChanged and fires the PropertyChanged event for all relevant properties. As a note, settingMode=OneWay
andUpdateSourceTrigger=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
add a comment |
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 DataTemplate
s, 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 DataTemplate
s I used.
Can anyone help me?
c# wpf xaml mvvm inotifypropertychanged
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 DataTemplate
s, 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 DataTemplate
s I used.
Can anyone help me?
c# wpf xaml mvvm inotifypropertychanged
c# wpf xaml mvvm inotifypropertychanged
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 fromObjects
, it should be sufficient that the array element implements INotifyPropertyChanged and fires the PropertyChanged event for all relevant properties. As a note, settingMode=OneWay
andUpdateSourceTrigger=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
add a comment |
1
Unless you don't add or remove objects fromObjects
, it should be sufficient that the array element implements INotifyPropertyChanged and fires the PropertyChanged event for all relevant properties. As a note, settingMode=OneWay
andUpdateSourceTrigger=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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53212282%2finotifypropertychanged-not-updating-ui-in-datatemplate-xaml-wpf%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
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, settingMode=OneWay
andUpdateSourceTrigger=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