Dependency property not raising when value assinged in view model

Babu R 61 Reputation points
2020-05-29T17:54:20.277+00:00

Hai

I am trying to create a custom datagrid control with a property called SelectedItem and I am trying to assign some object to this property in my viewmodel.
My codes are

Custom control

        public object SelectedItem
        {
            get => GetValue(selecteditemProperty);
            set => SetValue(selecteditemProperty, value);
        }
        public static readonly DependencyProperty selecteditemProperty = DependencyProperty.Register("SelectedItem", typeof(object), typeof(IGridListBoxColumn), new PropertyMetadata(new PropertyChangedCallback(SelectedItem_Changed)));
        private static void SelectedItem_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            IGridListBoxColumn mycontrol = sender as IGridListBoxColumn;
            mycontrol.callSelectedItem(e);
            MessageBox.Show("selected triggered");
            //not working
        }
        private void callSelectedItem(DependencyPropertyChangedEventArgs e)
        {
            MessageBox.Show("selected triggered");
        }

Mainwindow

private void Journal_Click(object sender, RoutedEventArgs e)
        {
            JournalViewModel Jvm = new JournalViewModel();
            Journal Jrn = new Journal();
            Jrn.DataContext = Jvm;
            MainFrame.Content = Jrn;
        }

Journal Page

    <Page.Resources>
        <CollectionViewSource x:Key="AcHeadList" Source="{Binding Tlb_Achead}"/>
    </Page.Resources>            

        <MyControls:IGrid.Columns>
                        <MyControls:IGridListBoxColumn Header="A/c Head" ItemsSource="{Binding Source={StaticResource AcHeadList}}" DisplayMember="Achead" SelectedItem ="{Binding **M_Tlb_Achead_Selected**, Mode=TwoWay}" Width="150"/>
                    </MyControls:IGrid.Columns>

viewmodel

Ob_JournalModel = new ObservableCollection<JournalModel>();

JournalModel Jm = new JournalModel();
Jm.**M_Tlb_Achead_Selected** = new ICMaster { Achead = value.M_Achead, CMaster_Id = value.M_CMaster_Id };

NotifyPropertyChanged("Ob_JournalModel");

Here When I assign value to M_Tlb_Achead_Selected I want to do some thing inside my dependency property.
please help me how to do this.

thanks

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,174 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Peter Fleischer (Freelancer) 17,571 Reputation points
    2020-06-02T12:02:20.867+00:00

    Hi, I copy your code in Window and Page and it works fine:

    <Window x:Class="WpfApp1.Window40"
            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:WpfApp1"
            mc:Ignorable="d"
            Title="Window40" Height="450" Width="800">
      <Grid>
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto"/>
          <RowDefinition Height="Auto"/>
          <RowDefinition/>
        </Grid.RowDefinitions>
        <Button Content="Show Journal" Click="Journal_Click"/>
        <Button Grid.Row="1" Content="Set item" Click="Button_Click" />
        <Frame Grid.Row="2" x:Name="MainFrame"/>
      </Grid>
    </Window>
    

    CodeBehind Main Window and ViewModel:

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Windows;
    using WpfApp40;
    
    namespace WpfApp1
    {
      /// <summary>
      /// Interaction logic for Window40.xaml
      /// </summary>
      public partial class Window40 : Window
      {
        public Window40()
        {
          InitializeComponent();
        }
    
        Window40Journal Jrn;
    
        private void Journal_Click(object sender, RoutedEventArgs e)
        {
          JournalViewModel Jvm = new JournalViewModel();
          Jrn = new Window40Journal();
          Jrn.DataContext = Jvm;
          MainFrame.Content = Jrn;
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
          if (Jrn != null) Jrn.SelectedItem = "x";
        }
      }
    }
    
    namespace WpfApp40
    {
      public class JournalViewModel
      {
        public JournalViewModel()
        {
          NameList = new ObservableCollection<MarKList>(db.MarKLists.ToList());
          Tlb_Achead = new ObservableCollection<Student>(db.StudentList.ToList());
        }
        Model db = new Model();
        public ObservableCollection<MarKList> NameList { get; set; }
        public ObservableCollection<Student> Tlb_Achead { get; set; }
      }
      public class Student
      {
        public string M_Name { get; set; }
        public int M_GenderID { get; set; }
        public string M_Mark1 { get; set; }
        public int M_Total { get; set; }
      }
      public class MarKList
      {
        public string Name { get; set; }
        public int ID { get; set; }
      }
      internal class Model
      {
        Random rnd = new Random();
        internal IEnumerable<MarKList> MarKLists
        {
          get
          {
            var l = new List<MarKList>();
            l.Add(new MarKList() { ID = 1, Name = "Male" });
            l.Add(new MarKList() { ID = 2, Name = "Female" });
            l.Add(new MarKList() { ID = 3, Name = "TransGender" });
            return l;
          }
        }
        internal IEnumerable<Student> StudentList { get { for (int i = 0; i < 10; i++) yield return new Student() { M_Name = $"Name {i}", M_GenderID = rnd.Next(1, 4), M_Mark1 = $"Mark1 {i}", M_Total = i }; } }
      }
    }
    

    Page XAML:

    <Page x:Class="WpfApp40.Window40Journal"
          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:WpfApp40"
          mc:Ignorable="d" 
          d:DesignHeight="450" d:DesignWidth="800"
          Title="Window40Journal">
      <Page.Resources>
        <CollectionViewSource x:Key="AcHeadList" Source="{Binding Tlb_Achead}"/>
      </Page.Resources>
      <DataGrid ItemsSource="{Binding Source={StaticResource AcHeadList}}"/>
    </Page>
    

    Page CodeBehind:

    using System.Windows;
    using System.Windows.Controls;
    
    namespace WpfApp40
    {
      public partial class Window40Journal : Page, IGridListBoxColumn
      {
        public Window40Journal()
        {
          InitializeComponent();
        }
    
        public object SelectedItem
        {
          get => GetValue(selecteditemProperty);
          set => SetValue(selecteditemProperty, value);
        }
        public static readonly DependencyProperty selecteditemProperty = 
          DependencyProperty.Register("SelectedItem", typeof(object), typeof(Window40Journal), 
            new PropertyMetadata(new PropertyChangedCallback(SelectedItem_Changed)));
        private static void SelectedItem_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
          IGridListBoxColumn mycontrol = sender as IGridListBoxColumn;
          mycontrol.callSelectedItem(e);
          MessageBox.Show("selected triggered");
          //not working
        }
        void IGridListBoxColumn.callSelectedItem(DependencyPropertyChangedEventArgs e)
        {
          MessageBox.Show("selected triggered");
        }
      }
    
      internal interface IGridListBoxColumn
      {
        void callSelectedItem(DependencyPropertyChangedEventArgs e);
      }
    }
    
    No comments