Dear Richard, many thanks for your help. Now it works. And I think I understand the functionality of DependencyProperty much better now. And sorry for the GitHub & 7z. I'm very new there.
iwangoll
Posts
-
DependencyProperty fires only in ViewModel.ctor -
DependencyProperty fires only in ViewModel.ctorThank you for your answer. I have changed my test project and adapted the original message. 1. removed all Debug.Writeline in TestControl 2. in ViewModel
set { SetProperty(ref _testInfoView, value); }
3. Added a button BtnShow to see in it's Click in xaml.cs the content of TestInfo property In the original post under "After starting the app" I'd explained what I've done. But the ViewModel-command didn't change the property. I've added the code to GitHub - iwangoll/TestBinding: ViewModel-command didn't change the DependencyProperty.[^]
-
DependencyProperty fires only in ViewModel.ctorHello, I'd tried to reduce the problem. I've this UserControl
public class TestControl : Label
{
public static readonly DependencyProperty dependencyPropertyTestInfo =
DependencyProperty.Register("TestInfo", typeof(string), typeof(TestControl),
new PropertyMetadata("", new PropertyChangedCallback(ChangeTestInfo)));public string TestInfo { get => (string)GetValue(dependencyPropertyTestInfo); set => SetValue(dependencyPropertyTestInfo, value); } private static void ChangeTestInfo(DependencyObject d, DependencyPropertyChangedEventArgs e) { var testControl = (TestControl)d; testControl.TestInfo = (string)e.NewValue; }
}
This is the essential part of MainWindow.xaml:
Its bounded in MainWindow.xaml.cs to DataContext with this ViewModel:
public class MainWindowViewModel : BindableBase
{
private int counter;
private string _testInfoView;
public string TestInfoView
{
get { return _testInfoView; }
set { SetProperty(ref _testInfoView, value); }
}
public DelegateCommand NewTestInfoCommand { get; }
public MainWindowViewModel()
{
NewTestInfoCommand = new DelegateCommand(OnNewTestInfoCommand);
TestInfoView = "Info from ctor";
}
private void OnNewTestInfoCommand()
{
string newInfo = $"Info No. {++counter}";
Debug.WriteLine($"{MethodBase.GetCurrentMethod().Name}: {nameof(TestInfoView)}=«{TestInfoView}» -> «{newInfo}»");
TestInfoView = newInfo;
}
}I've added a "Debug.WriteLine" in MainWindow.xaml.cs
private void BtnShow_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine($"{MethodBase.GetCurrentMethod().Name}: {nameof(tct.TestInfo)}={tct.TestInfo}");
}After starting the app 1. I've clicked "BtnShow" this is the debug-info: BtnShow_Click: TestInfo=Info from ctor 2. I've clicked "BtnNewTestInfo" this is the debug-info: OnNewTestInfoCommand: TestInfoView=«Info from ctor» -> «Info No. 1» 3. I've clicked "BtnShow" this is the debug-info: BtnShow_Click: TestInfo=Info from ctor Now my question is: why View