First steps for showing the source of value inheritance

上级 b30c027a
...@@ -10,12 +10,13 @@ ...@@ -10,12 +10,13 @@
using System.Windows; using System.Windows;
using System.Windows.Media; using System.Windows.Media;
using Snoop.Infrastructure; using Snoop.Infrastructure;
using Snoop.Infrastructure.Helpers;
public class InheritedValueSourceHelper : INotifyPropertyChanged public class InheritedValueSourceHelper : INotifyPropertyChanged
{ {
public static readonly InheritedValueSourceHelper Instance = new(); public static readonly InheritedValueSourceHelper Instance = new();
private static readonly PropertyInfo? inheritanceParentPropertyInfo = typeof(DependencyObject).GetProperty("InheritanceParent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); //private static readonly PropertyInfo? inheritanceParentPropertyInfo = typeof(DependencyObject).GetProperty("InheritanceParent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
public event PropertyChangedEventHandler? PropertyChanged; public event PropertyChangedEventHandler? PropertyChanged;
...@@ -40,19 +41,21 @@ private void CommandExecute(object? obj) ...@@ -40,19 +41,21 @@ private void CommandExecute(object? obj)
var sources = GetInheritedValueSources(currentTarget, property); var sources = GetInheritedValueSources(currentTarget, property);
MessageBox.Show(string.Join(Environment.NewLine, sources.Select(x => $"{UIObjectNameHelper.GetName(x)}"))); MessageBox.Show(string.Join(Environment.NewLine, sources.Select(x => $"{UIObjectNameHelper.GetNameAndType(x)}")));
} }
private static ObservableCollection<DependencyObject> GetInheritedValueSources(DependencyObject? currentTarget, DependencyProperty property) private static ObservableCollection<DependencyObject> GetInheritedValueSources(DependencyObject? toInspect, DependencyProperty property)
{ {
var sources = new ObservableCollection<DependencyObject>(); var sources = new ObservableCollection<DependencyObject>();
var currentTarget = toInspect;
if (currentTarget is not null) if (currentTarget is not null)
{ {
sources.Add(currentTarget); sources.Add(currentTarget);
} }
while ((currentTarget = inheritanceParentPropertyInfo?.GetValue(currentTarget) as DependencyObject) is not null) //while ((currentTarget = inheritanceParentPropertyInfo?.GetValue(currentTarget) as DependencyObject) is not null)
while (currentTarget is not null)
{ {
sources.Add(currentTarget); sources.Add(currentTarget);
...@@ -60,6 +63,9 @@ private static ObservableCollection<DependencyObject> GetInheritedValueSources(D ...@@ -60,6 +63,9 @@ private static ObservableCollection<DependencyObject> GetInheritedValueSources(D
{ {
break; break;
} }
currentTarget = LogicalTreeHelper.GetParent(currentTarget)
?? VisualTreeHelper.GetParent(currentTarget);
} }
return sources; return sources;
......
<!-- <!--
(c) Copyright Cory Plotts. (c) Copyright Cory Plotts.
This source is subject to the Microsoft Public License (Ms-PL). This source is subject to the Microsoft Public License (Ms-PL).
Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
<controls:Inspector x:Class="Snoop.Controls.PropertyGrid2" <controls:Inspector x:Class="Snoop.Controls.PropertyGrid2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:commands="clr-namespace:Snoop.Core.Commands"
xmlns:controls="clr-namespace:Snoop.Controls" xmlns:controls="clr-namespace:Snoop.Controls"
xmlns:converters="clr-namespace:Snoop.Converters" xmlns:converters="clr-namespace:Snoop.Converters"
xmlns:infrastructure="clr-namespace:Snoop.Infrastructure" xmlns:infrastructure="clr-namespace:Snoop.Infrastructure"
...@@ -100,12 +101,19 @@ ...@@ -100,12 +101,19 @@
<DataTemplate x:Key="ValueSourceTemplate" <DataTemplate x:Key="ValueSourceTemplate"
DataType="infrastructure:PropertyInformation"> DataType="infrastructure:PropertyInformation">
<TextBlock Height="16" <StackPanel Orientation="Horizontal">
Text="{Binding ValueSourceBaseValueSource}"> <TextBlock Text="{Binding ValueSourceBaseValueSource}">
<TextBlock.ToolTip> <TextBlock.ToolTip>
<TextBlock Text="{Binding ValueSourceBaseValueSource}" /> <TextBlock Text="{Binding ValueSourceBaseValueSource}" />
</TextBlock.ToolTip> </TextBlock.ToolTip>
</TextBlock> </TextBlock>
<Button Margin="2 0 0 0"
Command="{Binding Path=Command, Source={x:Static commands:InheritedValueSourceHelper.Instance}}"
CommandParameter="{Binding}"
Content="O"
Visibility="{Binding ValueSourceBaseValueSource, Converter={x:Static converters:EqualsToVisibilityConverter.DefaultInstance}, ConverterParameter=Inherited}" />
</StackPanel>
</DataTemplate> </DataTemplate>
</ResourceDictionary> </ResourceDictionary>
</Grid.Resources> </Grid.Resources>
......
...@@ -5,6 +5,16 @@ ...@@ -5,6 +5,16 @@
public static class UIObjectNameHelper public static class UIObjectNameHelper
{ {
public static string GetNameAndType(DependencyObject? dependencyObject)
{
if (dependencyObject is null)
{
return string.Empty;
}
return $"{GetName(dependencyObject)} ({dependencyObject.GetType().Name})";
}
public static string GetName(DependencyObject? dependencyObject) public static string GetName(DependencyObject? dependencyObject)
{ {
var result = string.Empty; var result = string.Empty;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册