Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit c4d1b0c9 authored by Markus QUARITSCH's avatar Markus QUARITSCH
Browse files

getting started with WPF and XAML

parent f6bec41f
No related branches found
No related tags found
No related merge requests found
Showing
with 1422 additions and 29 deletions
......@@ -29,12 +29,26 @@
* Martin Rexeis, rexeis@ivt.tugraz.at, IVT, Graz University of Technology
*/
namespace HashingTool
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App
{
}
}
using System.Runtime.Remoting.Contexts;
using System.Windows;
using HashingTool.ViewModel;
using HashingTool.Views;
namespace HashingTool
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
//protected override void OnStartup(StartupEventArgs e)
//{
// base.OnStartup(e);
// var app = new MainWindow();
// var context = new MainWindowViewModel();
// app.DataContext = context;
// //app.Show();
//}
}
}
......@@ -63,11 +63,25 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Model\TestModel.cs" />
<Compile Include="Properties\Annotations.cs" />
<Compile Include="Properties\Version.cs">
<DependentUpon>Version.tt</DependentUpon>
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
</Compile>
<Compile Include="ViewModel\IMainView.cs" />
<Compile Include="ViewModel\MainWindowViewModel.cs" />
<Compile Include="ViewModel\ObservableObject.cs" />
<Compile Include="ViewModel\RelayCommand.cs" />
<Compile Include="ViewModel\HashComponentDataViewModel.cs" />
<Compile Include="ViewModel\VerifyInputDataViewModel.cs" />
<Compile Include="Views\HashComponentData.xaml.cs">
<DependentUpon>HashComponentData.xaml</DependentUpon>
</Compile>
<Compile Include="Views\VerifyInputData.xaml.cs">
<DependentUpon>VerifyInputData.xaml</DependentUpon>
</Compile>
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
......@@ -80,6 +94,14 @@
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Page Include="Views\HashComponentData.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Views\VerifyInputData.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">
......
<Window x:Class="HashingTool.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="VECTO Hashing Tool" Height="422" Width="738">
<Grid>
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModel="clr-namespace:HashingTool.ViewModel"
xmlns:views="clr-namespace:HashingTool.Views"
x:Class="HashingTool.MainWindow"
Title="VECTO Hashing Tool" Height="552" Width="593">
<Window.DataContext>
<viewModel:MainWindowViewModel />
</Window.DataContext>
<Window.Resources>
<DataTemplate DataType="{x:Type viewModel:HashComponentDataViewModel}">
<views:HashComponentData />
</DataTemplate>
<DataTemplate DataType="{x:Type viewModel:VerifyInputDataViewModel}">
<views:VerifyInputData />
</DataTemplate>
</Window.Resources>
<Grid Margin="10">
<DockPanel>
<Border DockPanel.Dock="Left" BorderBrush="Black" BorderThickness="0,0,1,0">
<ItemsControl ItemsSource="{Binding MainViewModels}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid Margin="0,0,0,5">
<Button Content="{Binding Name}" Margin="2,5"
Command="{Binding DataContext.ChangeViewCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
CommandParameter="{Binding }" />
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border>
<ContentControl Content="{Binding CurrentViewModel}" />
</DockPanel>
</Grid>
</Window>
</Window>
\ No newline at end of file
......@@ -29,16 +29,16 @@
* Martin Rexeis, rexeis@ivt.tugraz.at, IVT, Graz University of Technology
*/
namespace HashingTool
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}
\ No newline at end of file
namespace HashingTool
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}
This diff is collapsed.
using System;
using System.ComponentModel;
using HashingTool.Model;
namespace HashingTool.ViewModel
{
public class HashComponentDataViewModel : ObservableObject, IMainView
{
private MainWindowViewModel _homeView;
public HashComponentDataViewModel(MainWindowViewModel homeView)
{
_homeView = homeView;
}
public string Name
{
get { return "Hash Component Data"; }
}
}
}
namespace HashingTool.ViewModel
{
public interface IMainView
{
string Name { get; }
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
using HashingTool.Views;
namespace HashingTool.ViewModel
{
public class MainWindowViewModel : ObservableObject
{
private ICommand _changeViewCommand;
private IMainView _currentView;
private List<IMainView> _availableViews;
private ICommand _homeView;
public MainWindowViewModel()
{
//var homeView = new HomeViewModel();
_availableViews = new List<IMainView> {
new HashComponentDataViewModel(this),
new VerifyInputDataViewModel(this)
};
CurrentViewModel = _availableViews[0];
_homeView = new RelayCommand(() => CurrentViewModel = _availableViews[0]);
}
public List<IMainView> MainViewModels
{
get { return _availableViews ?? (_availableViews = new List<IMainView>()); }
}
public IMainView CurrentViewModel
{
get { return _currentView; }
set {
if (_currentView == value) {
return;
}
_currentView = value;
RaisePropertyChanged("CurrentViewModel");
}
}
public ICommand ChangeViewCommand
{
get { return _changeViewCommand ?? (_changeViewCommand = new RelayCommand<IMainView>(ChangeViewModel)); }
}
public ICommand ShowHomeView
{
get { return _homeView; }
}
private void ChangeViewModel(IMainView mainView)
{
if (!MainViewModels.Contains(mainView)) {
return;
}
CurrentViewModel = MainViewModels.FirstOrDefault(mv => mv == mainView);
}
}
}
using System.ComponentModel;
namespace HashingTool.ViewModel
{
public abstract class ObservableObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
// Original author - Josh Smith - http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030
using System;
using System.Diagnostics;
using System.Windows.Input;
namespace HashingTool.ViewModel
{
/// <summary>
/// A command whose sole purpose is to relay its functionality to other objects by invoking delegates. The default return value for the CanExecute method is 'true'.
/// </summary>
public class RelayCommand<T> : ICommand
{
#region Declarations
readonly Predicate<T> _canExecute;
readonly Action<T> _execute;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand&lt;T&gt;"/> class.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action<T> execute, Predicate<T> canExecute = null)
{
if (execute == null) {
throw new ArgumentNullException("execute");
}
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
public event EventHandler CanExecuteChanged
{
add {
if (_canExecute != null) {
CommandManager.RequerySuggested += value;
}
}
remove {
if (_canExecute != null) {
CommandManager.RequerySuggested -= value;
}
}
}
[DebuggerStepThrough]
public Boolean CanExecute(Object parameter)
{
return _canExecute == null || _canExecute((T)parameter);
}
public void Execute(Object parameter)
{
_execute((T)parameter);
}
#endregion
}
/// <summary>
/// A command whose sole purpose is to relay its functionality to other objects by invoking delegates. The default return value for the CanExecute method is 'true'.
/// </summary>
public class RelayCommand : ICommand
{
#region Declarations
readonly Func<Boolean> _canExecute;
readonly Action _execute;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="RelayCommand&lt;T&gt;"/> class.
/// </summary>
/// <param name="execute">The execution logic.</param>
/// <param name="canExecute">The execution status logic.</param>
public RelayCommand(Action execute, Func<Boolean> canExecute = null)
{
if (execute == null) {
throw new ArgumentNullException("execute");
}
_execute = execute;
_canExecute = canExecute;
}
#endregion
#region ICommand Members
public event EventHandler CanExecuteChanged
{
add {
if (_canExecute != null) {
CommandManager.RequerySuggested += value;
}
}
remove {
if (_canExecute != null) {
CommandManager.RequerySuggested -= value;
}
}
}
[DebuggerStepThrough]
public Boolean CanExecute(Object parameter)
{
return _canExecute == null || _canExecute();
}
public void Execute(Object parameter)
{
_execute();
}
#endregion
}
}
namespace HashingTool.ViewModel
{
public class VerifyInputDataViewModel : ObservableObject, IMainView
{
public VerifyInputDataViewModel(MainWindowViewModel mainWindowViewModel) {}
public string Name
{
get { return "Verify Input Data"; }
}
}
}
<UserControl x:Class="HashingTool.Views.HashComponentData"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Label Content="Hash Component Data" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
</Grid>
</UserControl>
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 HashingTool.Views
{
/// <summary>
/// Interaction logic for HashComponentData.xaml
/// </summary>
public partial class HashComponentData : UserControl
{
public HashComponentData()
{
InitializeComponent();
}
}
}
<UserControl x:Class="HashingTool.Views.VerifyInputData"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Label Content="Verify Input Data" HorizontalAlignment="Left" Margin="131,115,0,0" VerticalAlignment="Top" />
</Grid>
</UserControl>
\ No newline at end of file
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 HashingTool.Views
{
/// <summary>
/// Interaction logic for VerifyInputData.xaml
/// </summary>
public partial class VerifyInputData : UserControl
{
public VerifyInputData()
{
InitializeComponent();
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment