WPFでWatermarkのメモ

More than 3 years have passed since last update.


WPFでWatermark(透かし)を使ったときのメモ


参照設定

Microsoft.TeamFoundation.Controls

※.NET Framework4.5


サンプル


MainWindowViewModel.cs

using Codeplex.Reactive;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace WatermarkSample
{
public class MainWindowViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

private string _name { get; set; }
private string _numberstring { get; set; }

public ReactiveProperty<string> Name { get; set; }
public ReactiveProperty<string> NumberString { get; set; }
public ReactiveCommand ClickCommand { get; private set; }

public ReactiveCollection<string> ComboBoxSource { get; private set; }

public MainWindowViewModel()
{
ComboBoxSource = Observable.Range(0, 100).Select(i => i.ToString()).ToReactiveCollection();

Name = ReactiveProperty.FromObject(this, o => o._name).ToReactiveProperty();
NumberString = ReactiveProperty.FromObject(this, o => o._numberstring).ToReactiveProperty();

ClickCommand = Name.CombineLatest(NumberString, (l, r) =>
{
return !string.IsNullOrEmpty(l) && !string.IsNullOrEmpty(r);
}).ToReactiveCommand();
ClickCommand.Subscribe(param =>
{
MessageBox.Show(string.Format("TextBox:{0}{1}ComboBox:{2}", Name.Value, Environment.NewLine, NumberString.Value));
});
}

protected void RaisePropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}



MainWindow.xaml

<Window x:Class="WatermarkSample.MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:WatermarkSample"
xmlns:wm="clr-namespace:Microsoft.TeamFoundation.Controls.WPF;assembly=Microsoft.TeamFoundation.Controls"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<l:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" Margin="142,84,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Text="{Binding Name.Value, Mode=TwoWay}">
<wm:Watermark.HintText>enter your id</wm:Watermark.HintText>
</TextBox>
<ComboBox HorizontalAlignment="Left" Margin="142,152,0,0" VerticalAlignment="Top" Width="120" ItemsSource="{Binding ComboBoxSource, Mode=OneTime}" SelectedIndex="{Binding NumberString.Value,Mode=TwoWay}">
<wm:Watermark.HintText>select</wm:Watermark.HintText>
</ComboBox>
<Button Content="Button" HorizontalAlignment="Left" Margin="142,211,0,0" VerticalAlignment="Top" Width="75" Command="{Binding ClickCommand}"/>
</Grid>
</Window>