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);
}
}