Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created February 6, 2014 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podhmo/8837263 to your computer and use it in GitHub Desktop.
Save podhmo/8837263 to your computer and use it in GitHub Desktop.
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:WpfApplication7"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<c:BindingData x:Key="bd" PreviewType="password"/>
</Window.Resources>
<StackPanel>
<StackPanel DataContext="{StaticResource bd}">
<TextBlock Text="{Binding PreviewType}"></TextBlock>
<TextBox Text="{Binding InputString, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBlock Text="{Binding InputString, Mode=OneWay}"></TextBlock>
<TextBlock Text="{Binding PreviewString, Mode=OneWay}"></TextBlock>
</StackPanel>
<StackPanel>
<StackPanel.DataContext>
<c:BindingData PreviewType="raw"/>
</StackPanel.DataContext>
<TextBlock Text="{Binding PreviewType}"></TextBlock>
<TextBox Text="{Binding InputString, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBlock Text="{Binding InputString, Mode=OneWay}"></TextBlock>
<TextBlock Text="{Binding PreviewString, Mode=OneWay}"></TextBlock>
</StackPanel>
</StackPanel>
</Window>
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 WpfApplication7
{
public enum PreviewType {
raw,
password
}
public class BindingData : DependencyObject
{
public static readonly DependencyProperty PreviewTypeProperty = DependencyProperty.Register(
"PreviewType", typeof(PreviewType), typeof(BindingData), new PropertyMetadata(PreviewType.raw));
public PreviewType PreviewType {
get { return (PreviewType)this.GetValue(PreviewTypeProperty); }
set { this.SetValue(PreviewTypeProperty, value); }
}
public static readonly DependencyProperty InputStringProperty = DependencyProperty.Register(
"InputString", typeof(string), typeof(BindingData), new FrameworkPropertyMetadata(new PropertyChangedCallback(OnInputStringPropertyChanged)));
private static void OnInputStringPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var s = e.NewValue as string;
switch ((PreviewType)d.GetValue(PreviewTypeProperty))
{
case PreviewType.password:
d.SetValue(PreviewStringProperty, new String((s.Select(c => '*').ToArray<char>())));
break;
case PreviewType.raw:
d.SetValue(PreviewStringProperty, s);
break;
}
}
public string InputString
{
get { return (string)this.GetValue(InputStringProperty); }
set { this.SetValue(InputStringProperty, value); }
}
public static readonly DependencyProperty PreviewStringProperty = DependencyProperty.Register(
"PreviewString", typeof(string), typeof(BindingData), new PropertyMetadata(""));
public string PreviewString
{
get { return (string)this.GetValue(PreviewStringProperty); }
set { this.SetValue(PreviewStringProperty, value); }
}
}
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment