Click here to Skip to main content
15,886,873 members
Home / Discussions / WPF
   

WPF

 
PinnedHOW TO ANSWER A QUESTION PinPopular
Chris Maunder16-Jul-09 3:09
cofounderChris Maunder16-Jul-09 3:09 
PinnedHow to get an answer to your question PinPopular
Chris Maunder16-Jul-09 3:05
cofounderChris Maunder16-Jul-09 3:05 
QuestionDataGrid Built In ViewModel Question Pin
Kevin Marois8-Apr-24 13:24
professionalKevin Marois8-Apr-24 13:24 
AnswerRe: DataGrid Built In ViewModel Question Pin
Mycroft Holmes10-Apr-24 12:04
professionalMycroft Holmes10-Apr-24 12:04 
QuestionWPF .Net 6 Pin
Kevin Marois14-Mar-24 16:35
professionalKevin Marois14-Mar-24 16:35 
AnswerRe: WPF .Net 6 Pin
Pete O'Hanlon14-Mar-24 20:22
mvePete O'Hanlon14-Mar-24 20:22 
QuestionDependencyProperty fires only in ViewModel.ctor Pin
iwangoll25-Feb-24 7:34
iwangoll25-Feb-24 7:34 
Hello,
I'd tried to reduce the problem.
I've this UserControl
C#
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:
<StackPanel>
    <local:TestControl Content="TestControl" x:Name="tct" TestInfo="{Binding TestInfoView}"/>
    <Button Content="show TestInfo by Button-Click in xaml.cs" x:Name="BtnShow" Click="BtnShow_Click"/>
    <Button Content="set new TestInfo" Name="BtnNewTestInfo" Command="{Binding NewTestInfoCommand}"/>
</StackPanel>

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 ViewModel-Command doesn't change the content of TestControl?

Explanation of the goal:
Based on a WrapPanel I'll add in Children ToggleButton as a representation of tags in a Control named "TagPanel". By clicking the ToggleButton I can define the Tags of a given dataset record.
To define the ToggleButton I'll set in ViewModel a string property "TagDef" with comma-separated values like "red,blue,green" to get three buttons.
And by setting a string property "CheckedTag" (i.e "green,red") I'll set IsChecked of the concerned buttons.

But by setting in viewmodel, the properties aren't changed in TagPanel. Now I'm using events - what a pity.

modified 26-Feb-24 7:39am.

AnswerRe: DependencyProperty fires only in ViewModel.ctor Pin
Richard Deeming25-Feb-24 22:44
mveRichard Deeming25-Feb-24 22:44 
GeneralRe: DependencyProperty fires only in ViewModel.ctor Pin
iwangoll26-Feb-24 1:51
iwangoll26-Feb-24 1:51 
GeneralRe: DependencyProperty fires only in ViewModel.ctor Pin
Richard Deeming26-Feb-24 2:19
mveRichard Deeming26-Feb-24 2:19 
GeneralRe: DependencyProperty fires only in ViewModel.ctor Pin
iwangoll26-Feb-24 3:16
iwangoll26-Feb-24 3:16 
QuestionGet Tab For Context Menu Pin
Kevin Marois26-Jan-24 18:35
professionalKevin Marois26-Jan-24 18:35 
QuestionINotifyDataErrorInfo Question Pin
Kevin Marois2-Jan-24 17:07
professionalKevin Marois2-Jan-24 17:07 
QuestionWhen Are All DependencyProperties Set Pin
Kevin Marois28-Dec-23 21:12
professionalKevin Marois28-Dec-23 21:12 
AnswerRe: When Are All DependencyProperties Set Pin
#realJSOP9-Apr-24 1:30
mve#realJSOP9-Apr-24 1:30 
QuestionDataTemplate Problem Pin
Kevin Marois5-Dec-23 12:08
professionalKevin Marois5-Dec-23 12:08 
AnswerRe: DataTemplate Problem Pin
Richard Deeming5-Dec-23 22:03
mveRichard Deeming5-Dec-23 22:03 
AnswerDataGrid Event Not Firing Pin
Kevin Marois30-Nov-23 13:11
professionalKevin Marois30-Nov-23 13:11 
GeneralRe: DataGrid Event Not Firing Pin
Andre Oosthuizen1-Dec-23 22:42
mveAndre Oosthuizen1-Dec-23 22:42 
GeneralRe: DataGrid Event Not Firing Pin
Kevin Marois2-Dec-23 7:41
professionalKevin Marois2-Dec-23 7:41 
QuestionDataGrid Exception Pin
Kevin Marois28-Nov-23 11:36
professionalKevin Marois28-Nov-23 11:36 
AnswerRe: DataGrid Exception Pin
Richard Deeming28-Nov-23 22:30
mveRichard Deeming28-Nov-23 22:30 
GeneralRe: DataGrid Exception Pin
Kevin Marois29-Nov-23 7:12
professionalKevin Marois29-Nov-23 7:12 
GeneralRe: DataGrid Exception Pin
Kevin Marois29-Nov-23 8:13
professionalKevin Marois29-Nov-23 8:13 
GeneralRe: DataGrid Exception Pin
Richard Deeming29-Nov-23 22:22
mveRichard Deeming29-Nov-23 22:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.