Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion CalcBinding/CalcBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ public override object ProvideValue(IServiceProvider serviceProvider)

BindingBase resBinding;

if (sourcePropertiesPathesWithPositions.Count() == 1)
if (sourcePropertiesPathesWithPositions.Count == 1 && targetPropertyType == typeof(System.Windows.Input.ICommand) && expressionTemplate == "{0}()")
{
var methodName = sourcePropertiesPathesWithPositions.Single().Item1;

var targetProvider = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget));
var targetObject = targetProvider.TargetObject as FrameworkElement;

return new DataContextMethodCommand(methodName, targetObject);
}
else if (sourcePropertiesPathesWithPositions.Count() == 1)
{
var binding = new System.Windows.Data.Binding(sourcePropertiesPathesWithPositions.Single().Item1)
{
Expand Down
1 change: 1 addition & 0 deletions CalcBinding/CalcBinding.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<Reference Include="WindowsBase" />
</ItemGroup>
<ItemGroup>
<Compile Include="DataContextMethodCommand.cs" />
<Compile Include="CalcBinding.cs" />
<Compile Include="CalcConverter.cs" />
<Compile Include="BoolToVisibilityConverter.cs" />
Expand Down
34 changes: 34 additions & 0 deletions CalcBinding/DataContextMethodCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Windows;
using System.Windows.Input;

namespace CalcBinding
{
public class DataContextMethodCommand : ICommand
{
string _methodName;
FrameworkElement _targetObject;

public DataContextMethodCommand(string methodName, FrameworkElement targetObject)
{
_methodName = methodName;
_targetObject = targetObject;
}

public event EventHandler CanExecuteChanged;

public bool CanExecute(object parameter)
{
return true;
}

public void Execute(object parameter)
{
//get DataContext as late as possible to execute on the correct instance
var dataContext = _targetObject.DataContext;
var dataContextType = dataContext.GetType();
var dataContextMethod = dataContextType.GetMethod(_methodName);
dataContextMethod.Invoke(dataContext, null);
}
}
}
30 changes: 30 additions & 0 deletions Tests/CalcBindingSystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,36 @@ public void BindingToPropertyContainingDigits()
);
}

[TestMethod]
public void BindingToMethod()
{
var viewModel1 = new ExampleViewModel();
var viewModel2 = new ExampleViewModel();
Assert.IsFalse(viewModel1.ClickMethodInvoked);
Assert.IsFalse(viewModel2.ClickMethodInvoked);

var button = new Button();
button.DataContext = viewModel1;

var calcBinding = new CalcBinding.Binding("ClickMethod()");
var bindingExpression = calcBinding.ProvideValue(new ServiceProviderMock(button, Button.CommandProperty));

button.SetValue(Button.CommandProperty, bindingExpression);
var command = button.GetValue(Button.CommandProperty) as System.Windows.Input.ICommand;
command.Execute(null);

Assert.IsTrue(viewModel1.ClickMethodInvoked);
Assert.IsFalse(viewModel2.ClickMethodInvoked);

button.DataContext = viewModel2;
command = button.GetValue(Button.CommandProperty) as System.Windows.Input.ICommand;
command.Execute(null);

Assert.IsTrue(viewModel1.ClickMethodInvoked);
Assert.IsTrue(viewModel2.ClickMethodInvoked);
}


#region Convert

public void StringAndObjectBindingAssert(string path, INotifyPropertyChanged source,
Expand Down
18 changes: 18 additions & 0 deletions WpfExample/ExampleViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@ public ConcreteViewModel2(int a)
/// </summary>
public class ExampleViewModel : BaseViewModel
{
private bool clickMethodInvoked;
public bool ClickMethodInvoked
{
get { return clickMethodInvoked; }
set
{
clickMethodInvoked = value;

new object().TraceTime(
() => RaisePropertyChanged(() => ClickMethodInvoked)
);
}
}
public void ClickMethod()
{
ClickMethodInvoked = !ClickMethodInvoked;
}

private double a = 10;
public double A
{
Expand Down
20 changes: 20 additions & 0 deletions WpfExample/FifthPage.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<UserControl x:Class="WpfExample.FifthPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:CalcBinding;assembly=CalcBinding"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:Height="450" d:Width="825">

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<CheckBox Grid.Row="0" Content="Button clicked" IsChecked="{Binding ClickMethodInvoked}" IsEnabled="False"/>
<Button Grid.Row="1" Content="Click" Command="{c:Binding ClickMethod()}"/>

</Grid>
</UserControl>
28 changes: 28 additions & 0 deletions WpfExample/FifthPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfExample
{
/// <summary>
/// Interaction logic for FifthPage.xaml
/// </summary>
public partial class FifthPage : UserControl
{
public FifthPage()
{
InitializeComponent();
}
}
}
13 changes: 8 additions & 5 deletions WpfExample/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
</TabItem>
<TabItem Header="Math and inversed expressions">
<local:ThirdPage/>
</TabItem>
<TabItem Header="Null properties support">
<local:FourthPage/>
</TabItem>
</TabControl>
</TabItem>
<TabItem Header="Null properties support">
<local:FourthPage/>
</TabItem>
<TabItem Header="Commands">
<local:FifthPage/>
</TabItem>
</TabControl>
</Grid>
</Window>
7 changes: 7 additions & 0 deletions WpfExample/WpfExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Page Include="FifthPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="FourthPage.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand All @@ -100,6 +104,9 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="FifthPage.xaml.cs">
<DependentUpon>FifthPage.xaml</DependentUpon>
</Compile>
<Compile Include="FourthPage.xaml.cs">
<DependentUpon>FourthPage.xaml</DependentUpon>
</Compile>
Expand Down