こんにちは。いつもお世話になります。
WPF + Livet + Livet Extensionで、Propertyの処理を共通化できるのでしょうか?
環境は、Window10, Visual Studio2015, WPF4.5, Livet1.3.0.0, LivetExtensions1.0.3.0)の組み合わせです。
下記のとおり、X, Yを入力する二つのtextboxがあります。
値がdoubleの数値か判定して値を入れようとしています。
XとYでPropertyの処理は共通ですが、ふたつにしないとだめですか?
Mothodならたとえば、
private bool Validation(TextBox textbox){
double v;
try{
v = double.Parse(textbox.Text);
return true;
}
catch {
return false;
}
}
とかの処理で、TextBox.TextChangedeventから呼べば、Validationはひとつのコードを使いまわせると思います。
Propertyだと、
C#
1 private double x; 2 3 public string XTextBoxText 4 { 5 get 6 { 7 return this.x.ToString(CultureInfo.InvariantCulture); 8 } 9 10 set 11 { 12 if (this.XTextBoxText == value) 13 { 14 return; 15 } 16 17 double v; 18 19 try 20 { 21 v = double.Parse(value); 22 } 23 catch 24 { 25 this.RaisePropertyChanged(() => this.XTextBoxText); 26 27 return; 28 } 29 30 this.x = v; 31 32 this.RaisePropertyChanged(() => this.XTextBoxText); 33 } 34 } 35 36```のコードのXをYに変えてもうひとつ作らないとだめなのでしょうか? 37 38 39```XAML 40<Window x:Class="Livet.Window" 41 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 42 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 43 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 44 xmlns:l="http://schemas.livet-mvvm.net/2011/wpf" 45 Height="250" Width="525" ResizeMode="NoResize"> 46 47 <i:Interaction.Triggers> 48 <i:EventTrigger EventName="ContentRendered"> 49 <l:LivetCallMethodAction MethodTarget="{Binding}" MethodName="Initialize"/> 50 </i:EventTrigger> 51 <i:EventTrigger EventName="Closed"> 52 <l:DataContextDisposeAction/> 53 </i:EventTrigger> 54 </i:Interaction.Triggers> 55 56 <Grid Margin="10"> 57 <Grid.ColumnDefinitions> 58 <ColumnDefinition Width="150"/> 59 <ColumnDefinition Width="*"/> 60 </Grid.ColumnDefinitions> 61 <Label Grid.Row="0" Grid.Column="0" Content="X" HorizontalAlignment="Left" FontFamily="Meiryo"/> 62 <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding XTextBoxText}" x:Name="LocationXTextBox" TextWrapping="NoWrap" FontFamily="Meiryo" FontSize="11"/> 63 <Label Grid.Row="1" Grid.Column="0" Content="Y" HorizontalAlignment="Left" FontFamily="Meiryo"/> 64 <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding YTextBoxText}" x:Name="LocationYTextBox" TextWrapping="NoWrap" FontFamily="Meiryo" FontSize="11"/> 65 </Grid> 66</Window>
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/07/14 01:55
2016/07/14 02:11 編集
2016/07/14 02:33
2016/07/14 02:39
2016/07/14 03:39
2016/07/14 04:08
2016/07/14 04:12
2016/07/14 04:33