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?”.
Last active
April 26, 2017 14:56
-
-
Save poke/bd9bcb6d1f1bf50b988ea8909edb99af to your computer and use it in GitHub Desktop.
StackOverflow 43623601 – Example code for answer http://stackoverflow.com/a/43628625/216074
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="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> |
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 WpfTest | |
{ | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
DataContext = new SimpleViewModel(); | |
} | |
} | |
} |
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.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