Skip to content

Commit 782d24e

Browse files
committed
Add BindingProxy to fix runtime binding errors
BYJRK/DotNet-Discussions#364
1 parent bb355dc commit 782d24e

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

IPConfig/Controls/BindingProxy.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System.Windows;
2+
3+
namespace IPConfig.Controls;
4+
5+
/// <summary>
6+
/// <see cref="[WPF] How to bind to data when the DataContext is not inherited">https://thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/</see>
7+
/// </summary>
8+
public class BindingProxy : Freezable
9+
{
10+
#region Overrides of Freezable
11+
12+
protected override Freezable CreateInstanceCore()
13+
{
14+
return new BindingProxy();
15+
}
16+
17+
#endregion Overrides of Freezable
18+
19+
// Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc...
20+
public static readonly DependencyProperty DataProperty =
21+
DependencyProperty.Register(
22+
"Data",
23+
typeof(object),
24+
typeof(BindingProxy),
25+
new UIPropertyMetadata(null));
26+
27+
public object Data
28+
{
29+
get => GetValue(DataProperty);
30+
set => SetValue(DataProperty, value);
31+
}
32+
}

IPConfig/Views/NicConfigDetailView.xaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:b="clr-namespace:IPConfig.Behaviors"
5+
xmlns:control="clr-namespace:IPConfig.Controls"
56
xmlns:conv="clr-namespace:IPConfig.Converters"
67
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
78
xmlns:hc="https://handyorg.github.io/handycontrol"
@@ -20,6 +21,9 @@
2021
IsVisibleChanged="UserControl_IsVisibleChanged"
2122
mc:Ignorable="d">
2223
<UserControl.Resources>
24+
<control:BindingProxy x:Key="Proxy"
25+
Data="{Binding}" />
26+
2327
<conv:CopyContentFormatConverter x:Key="CopyContentFormatConverter" />
2428
<conv:BytesToFileSizeConverter x:Key="BytesToFileSizeConverter" />
2529

@@ -108,7 +112,7 @@
108112

109113
<UserControl.InputBindings>
110114
<KeyBinding Key="Backspace"
111-
Command="{Binding GoBackCommand}" />
115+
Command="{Binding Data.GoBackCommand, Source={StaticResource Proxy}}" />
112116
</UserControl.InputBindings>
113117

114118
<Grid>

IPConfig/Views/NicInfoCardView.xaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:b="clr-namespace:IPConfig.Behaviors"
5+
xmlns:control="clr-namespace:IPConfig.Controls"
56
xmlns:conv="clr-namespace:IPConfig.Converters"
67
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
78
xmlns:hc="https://handyorg.github.io/handycontrol"
@@ -16,20 +17,23 @@
1617
d:DesignWidth="200"
1718
mc:Ignorable="d">
1819
<UserControl.Resources>
20+
<control:BindingProxy x:Key="Proxy"
21+
Data="{Binding}" />
22+
1923
<conv:NicIPConfigToolTipConverter x:Key="NicIPConfigToolTipConverter" />
2024
<conv:SelectedNicIPConfigNameConverter x:Key="SelectedNicIPConfigNameConverter" />
2125
</UserControl.Resources>
2226

2327
<UserControl.InputBindings>
2428
<KeyBinding Key="F11"
25-
Command="{Binding ViewNicIPConfigCommand}"
29+
Command="{Binding Data.ViewNicIPConfigCommand, Source={StaticResource Proxy}}"
2630
CommandParameter="{StaticResource True}" />
2731

2832
<KeyBinding Key="F12"
29-
Command="{Binding ViewToNicConfigDetailCommand}" />
33+
Command="{Binding Data.ViewToNicConfigDetailCommand, Source={StaticResource Proxy}}" />
3034

3135
<KeyBinding Key="Esc"
32-
Command="{Binding ViewNicIPConfigCommand}"
36+
Command="{Binding Data.ViewNicIPConfigCommand, Source={StaticResource Proxy}}"
3337
CommandParameter="{StaticResource False}" />
3438
</UserControl.InputBindings>
3539

IPConfig/Views/NicSelectorView.xaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:b="clr-namespace:IPConfig.Behaviors"
5+
xmlns:control="clr-namespace:IPConfig.Controls"
56
xmlns:conv="clr-namespace:IPConfig.Converters"
67
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
78
xmlns:hc="https://handyorg.github.io/handycontrol"
@@ -17,6 +18,9 @@
1718
d:DesignWidth="400"
1819
mc:Ignorable="d">
1920
<UserControl.Resources>
21+
<control:BindingProxy x:Key="Proxy"
22+
Data="{Binding}" />
23+
2024
<conv:OperationalStatusToolTipConverter x:Key="OpStatusToolTipConverter" />
2125
</UserControl.Resources>
2226

@@ -28,7 +32,7 @@
2832

2933
<UserControl.InputBindings>
3034
<KeyBinding Key="F5"
31-
Command="{Binding RefreshCommand}"
35+
Command="{Binding Data.RefreshCommand, Source={StaticResource Proxy}}"
3236
CommandParameter="{StaticResource True}" />
3337
</UserControl.InputBindings>
3438

0 commit comments

Comments
 (0)