Created
November 30, 2009 10:52
-
-
Save bolthar/245390 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Windows; | |
namespace WpfApplication1 | |
{ | |
public partial class App : Application | |
{ | |
protected override void OnStartup(StartupEventArgs e) | |
{ | |
base.OnStartup(e); | |
var viewmodel = new TestViewModel(new TestWindow()); | |
viewmodel.show(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using WpfApplication1; | |
namespace WpfApplication1 | |
{ | |
public class DummyCommand : ICommandHost<object> | |
{ | |
private TestDataModel _model; | |
public DummyCommand(TestDataModel model) | |
{ | |
_model = model; | |
} | |
public void Execute(object param) | |
{ | |
_model.Value = "Clicked!"; | |
} | |
public bool CanExecute(object param) | |
{ | |
return true; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace WpfApplication1 | |
{ | |
public interface ICommandHost<T> | |
{ | |
void Execute(T param); | |
bool CanExecute(T param); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel; | |
namespace WpfApplication1 | |
{ | |
public class TestDataModel : INotifyPropertyChanged | |
{ | |
private string _value; | |
public string Value | |
{ | |
get { return _value; } | |
set | |
{ | |
if (_value != value) | |
{ | |
_value = value; | |
if(PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Value")); | |
} | |
} | |
} | |
public event PropertyChangedEventHandler PropertyChanged; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows.Controls.Primitives; | |
using Microsoft.Practices.Composite.Wpf.Commands; | |
using WpfApplication1; | |
namespace WpfApplication1 | |
{ | |
public class TestViewModel | |
{ | |
private TestWindow _view; | |
public TestViewModel(TestWindow view) | |
{ | |
_view = view; | |
_view.DataContext = new TestDataModel(); | |
foreach (ButtonBase control in _view.GetCommands()) | |
{ | |
create_command(control); | |
} | |
} | |
private void create_command(ButtonBase control) | |
{ | |
Type command_host_type = Type.GetType(string.Format("WpfApplication1.{0}Command", control.Name)); | |
ICommandHost<object> command_host = (ICommandHost<object>)Activator.CreateInstance(command_host_type, new[] { _view.DataContext }); | |
DelegateCommand<object> command = new DelegateCommand<object>(command_host.Execute, command_host.CanExecute); | |
control.Command = command; | |
} | |
public void show() | |
{ | |
_view.Show(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="WpfApplication1.TestWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
Title="TestWindow" Height="300" Width="300"> | |
<StackPanel> | |
<Button Name="Dummy"> | |
Dummy command | |
</Button> | |
<TextBox Text="{Binding Path=Value}"></TextBox> | |
</StackPanel> | |
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using System.Windows; | |
using System.Windows.Controls.Primitives; | |
namespace WpfApplication1 | |
{ | |
public partial class TestWindow : Window | |
{ | |
public TestWindow() | |
{ | |
InitializeComponent(); | |
} | |
public IEnumerable GetCommands() | |
{ | |
//dummy | |
return new List<ButtonBase> {Dummy}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment