トップ回答者
定義はXAMLに残しつつ、データをコードビハインドから設定したい

質問
-
お世話になっております。
以下のようにXAMLでラベルを定義しています。
ラベル上に「こんにちは」と表示しています。
この文字を動的に変えたいのです。
その際、ラベルの定義はXAMLに残しつつ、設定したいメッセージだけコードビハインドに記述したいのです。
その具体的な記述が分かりません。
非常に初歩的な質問ですみません。。。
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Canvas> <Thumb DragDelta="DragDelta" Canvas.Left="12" Canvas.Top="12" Height="250" Width="250"> <Thumb.Template> <ControlTemplate> <Label Content="こんにちは☆" Width="80" Height="20" Background="Aqua" /> </ControlTemplate> </Thumb.Template> </Thumb> </Canvas> </Window>
回答
-
-
直接Label.Contentを書き換える方法はtrapemiyaさんが紹介されているので、私はBindingを使う方法を。
<Window x:Class="WpfApplication13.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Canvas> <Thumb DragDelta="DragDelta" Canvas.Left="12" Canvas.Top="12" Height="250" Width="250"> <Thumb.Template> <ControlTemplate> <Label Content="{Binding Path=LabelText}" Width="80" Height="50" Background="Aqua" /> </ControlTemplate> </Thumb.Template> </Thumb> </Canvas> </Window>
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new ViewModel(); } private void DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) { ((ViewModel)this.DataContext).TextChange(); } } class ViewModel:INotifyPropertyChanged { string _labelText; public string LabelText { get { return _labelText; } set { _labelText = value; OnPropertyChanged("LabelText"); } } public ViewModel() { this.LabelText = "こんにちは☆"; } public void TextChange() { switch (this.LabelText) { case "こんにちは☆": this.LabelText = "こんばんわ"; break; case "こんばんわ": this.LabelText = "おはようございます"; break; case "おはようございます": this.LabelText = "こんにちは☆"; break; } } protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; }
上記のようにLabel.ContentプロパティにViewModel.LabelTextプロパティをBindingします。
コード側での更新の反映はINotifyPropertyChangedインターフェースを経由してPropertyChangedイベントを発生させれば反映されます。
この形が(おそらく)WPFにおけるBindingの基本形です。WinFormとは全く考え方が違うので慣れないうちは面倒に感じるかもしれませんが、個人的にはBindingはWPFの基本であり最も重要な手法だと思いますので、ぜひ頑張って習得して下さい。
- 回答としてマーク sumi_sumi 2012年10月5日 3:51
すべての返信
-
-
直接Label.Contentを書き換える方法はtrapemiyaさんが紹介されているので、私はBindingを使う方法を。
<Window x:Class="WpfApplication13.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Canvas> <Thumb DragDelta="DragDelta" Canvas.Left="12" Canvas.Top="12" Height="250" Width="250"> <Thumb.Template> <ControlTemplate> <Label Content="{Binding Path=LabelText}" Width="80" Height="50" Background="Aqua" /> </ControlTemplate> </Thumb.Template> </Thumb> </Canvas> </Window>
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); this.DataContext = new ViewModel(); } private void DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e) { ((ViewModel)this.DataContext).TextChange(); } } class ViewModel:INotifyPropertyChanged { string _labelText; public string LabelText { get { return _labelText; } set { _labelText = value; OnPropertyChanged("LabelText"); } } public ViewModel() { this.LabelText = "こんにちは☆"; } public void TextChange() { switch (this.LabelText) { case "こんにちは☆": this.LabelText = "こんばんわ"; break; case "こんばんわ": this.LabelText = "おはようございます"; break; case "おはようございます": this.LabelText = "こんにちは☆"; break; } } protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } public event PropertyChangedEventHandler PropertyChanged; }
上記のようにLabel.ContentプロパティにViewModel.LabelTextプロパティをBindingします。
コード側での更新の反映はINotifyPropertyChangedインターフェースを経由してPropertyChangedイベントを発生させれば反映されます。
この形が(おそらく)WPFにおけるBindingの基本形です。WinFormとは全く考え方が違うので慣れないうちは面倒に感じるかもしれませんが、個人的にはBindingはWPFの基本であり最も重要な手法だと思いますので、ぜひ頑張って習得して下さい。
- 回答としてマーク sumi_sumi 2012年10月5日 3:51