Reputation: 1
the following problem drives me crazy and I hope someone can explain me what I am doing wrong. To do the notification and updating property values for views I am using the communitytoolkit.mvvm.
I have a ViewModel: TextViewModel represented by View TestView. The ViewModel contains an TestClass class and bool property named Inprogress
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System.Windows;
namespace Wpf_TestingEnvironment
{
public partial class TestViewModel : ObservableObject
{
[ObservableProperty]
private bool inprogress;
[ObservableProperty]
private TestClass testclass;
public TestViewModel()
{
Inprogress = false;
Testclass = new TestClass();
}
}
public partial class TestClass : ObservableObject
{
[RelayCommand]
private void DoSomething(bool parentinprogress)
{
MessageBox.Show("Did something");
parentinprogress = true;
}
public TestClass() { }
}
}
<Window x:Class="Wpf_TestingEnvironment.TestView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Wpf_TestingEnvironment"
mc:Ignorable="d"
Title="TestView" Height="450" Width="800">
<Window.DataContext>
<local:TestViewModel/>
</Window.DataContext>
<StackPanel Margin="5" Orientation="Vertical">
<local:UserControl1 Testclass="{Binding Testclass}"
ParentInProgress="{Binding Inprogress, Mode=TwoWay}" Height="200" Width="300"/>
<TextBlock Text="{Binding Inprogress}"/>
</StackPanel>
</Window>
A UserControl UserControl1 takes over the Testclass from ViewModel and the Inprogress property:
using System.Windows;
using System.Windows.Controls;
namespace Wpf_TestingEnvironment
{
public partial class UserControl1 : UserControl
{
public TestClass Testclass
{
get { return (TestClass)GetValue(TestclassProperty); }
set { SetValue(TestclassProperty, value); }
}
public static readonly DependencyProperty TestclassProperty =
DependencyProperty.Register("Testclass", typeof(TestClass), typeof(UserControl1), new PropertyMetadata(null));
public bool ParentInProgress
{
get { return (bool)GetValue(ParentInProgressProperty); }
set { SetValue(ParentInProgressProperty, value); }
}
public static readonly DependencyProperty ParentInProgressProperty =
DependencyProperty.Register("ParentInProgress", typeof(bool), typeof(UserControl1), new PropertyMetadata(false));
public UserControl1() { InitializeComponent(); }
}
}
<UserControl x:Class="Wpf_TestingEnvironment.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:Wpf_TestingEnvironment"
mc:Ignorable="d"
d:DesignHeight="200" d:DesignWidth="300" Name="uc">
<Grid Margin="5" Background="LightGreen">
<Button Width="100" Height="100" Content="TestButton" Command="{Binding ElementName=uc, Path=Testclass.DoSomethingCommand}"
CommandParameter="{Binding ElementName=uc, Path=ParentInProgress, Mode=TwoWay}"/>
</Grid>
</UserControl>
I am trying to do a task via relaycommand DoSomething(bool parentinprogress) in TestClass method and changing the ViewModels property Inprogress to true during that method.
The implementation of the DoSomething methode shall be in UserControl1 because I would like to use that kind of usercontrol in another viewmodel (solutions project)...
In case I debug the DoSomething methode I can see the current value of parentinprogress.
Any changes of the commandparameter parentinprogress in TestClass methode DoSomething do not effect the ViewModels property Inprogress, althrough the commandparameter is bond to eachothers.
Upvotes: 0
Views: 35
Reputation: 1
Like suggested I added the following Button_Click event to the UserControl1.xaml and it is working like intended:
private void Button_Click(object sender, RoutedEventArgs e)
{
ParentInProgress = true;
Testclass.DoSomethingCommand.Execute(true);
}
Upvotes: 0