Skip to content

Instantly share code, notes, and snippets.

@poke
Last active April 26, 2017 14:56
Show Gist options
  • Save poke/bd9bcb6d1f1bf50b988ea8909edb99af to your computer and use it in GitHub Desktop.
Save poke/bd9bcb6d1f1bf50b988ea8909edb99af to your computer and use it in GitHub Desktop.
StackOverflow 43623601 – Example code for answer http://stackoverflow.com/a/43628625/216074

Example code for my answer on the Stack Overflow question “In a WPF control that acts as a container, how do I place the content?”.

<ContentControl
x:Class="WpfTest.EllipsisButtonControl"
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"
xmlns:local="clr-namespace:WpfTest"
d:DesignHeight="30" d:DesignWidth="300" mc:Ignorable="d">
<ContentControl.Template>
<ControlTemplate TargetType="ContentControl">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="0"
Content="{TemplateBinding Content}" />
<Button Grid.Column="1" Content="Press me"
Command="{TemplateBinding local:EllipsisButtonControl.Command}" />
</Grid>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WpfTest
{
public partial class EllipsisButtonControl : ContentControl
{
public EllipsisButtonControl()
{
InitializeComponent();
}
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(EllipsisButtonControl), new PropertyMetadata(null));
}
}
<Window x:Class="WpfTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTest"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:EllipsisButtonControl Command="{Binding Test}">
<TextBlock Text="{Binding Content}" />
</local:EllipsisButtonControl>
</Grid>
</Window>
using System.Windows;
namespace WpfTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new SimpleViewModel();
}
}
}
using System;
using System.ComponentModel;
using System.Windows.Input;
namespace WpfTest
{
public class SimpleViewModel : INotifyPropertyChanged
{
private int counter = 0;
public event PropertyChangedEventHandler PropertyChanged;
public ICommand Test { get; }
public string Content { get; set; } = "Initial text";
public SimpleViewModel()
{
Test = new SimpleRelayCommand(() =>
{
counter++;
Content = $"Button pressed {counter} times";
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Content)));
});
}
public class SimpleRelayCommand : ICommand
{
private readonly Action _action;
public SimpleRelayCommand(Action action)
{
_action = action;
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) => true;
public void Execute(object parameter) => _action();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment