[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
using System.ComponentModel; namespace SilverlightDataTriggerDemo { public class MyViewModel:ViewModel { private bool isViewModelEnabled; public bool IsViewModelEnabled { get { return isViewModelEnabled; } set { isViewModelEnabled = value; NotifyPropertyChanged("IsViewModelEnabled"); } } } public class ViewModel:INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged = delegate { }; protected void NotifyPropertyChanged(string propertyName) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } }
<UserControl x:Class="SilverlightDataTriggerDemo.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="clr-namespace:SilverlightDataTriggerDemo" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <UserControl.Resources> <vm:MyViewModel x:Key="viewModel"/> </UserControl.Resources> <CheckBox DataContext="{StaticResource viewModel}" IsChecked="{Binding IsViewModelEnabled, Mode=TwoWay}"> <TextBlock> <i:Interaction.Triggers> <ei:DataTrigger Binding="{Binding IsViewModelEnabled}" Value="true"> <ei:ChangePropertyAction PropertyName="Text" Value="Changed!!"/> </ei:DataTrigger> </i:Interaction.Triggers> </TextBlock> </CheckBox> </UserControl>
<UserControl x:Class="SilverlightDataTriggerDemo.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:vm="clr-namespace:SilverlightDataTriggerDemo" xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:ei="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <UserControl.Resources> <vm:MyViewModel x:Key="viewModel"/> </UserControl.Resources> <CheckBox DataContext="{StaticResource viewModel}" IsChecked="{Binding IsViewModelEnabled, Mode=TwoWay}"> <TextBlock> <i:Interaction.Triggers> <ei:DataTrigger Binding="{Binding IsViewModelEnabled}" Value="true"> <ei:ChangePropertyAction PropertyName="Text" Value="Changed!!"/> </ei:DataTrigger> <ei:DataTrigger Binding="{Binding IsViewModelEnabled}" Value="false"> <ei:ChangePropertyAction PropertyName="Text" Value=""/> </ei:DataTrigger> </i:Interaction.Triggers> </TextBlock> </CheckBox> </UserControl>
using System.Windows.Input; namespace WpfApplication1 { public class TestViewModel { public DelegateCommand SqrtCommand { get; private set; } public double Number { get; set; } public TestViewModel() { SqrtCommand = new DelegateCommand( () => { System.Windows.MessageBox.Show(System.Math.Sqrt(Number).ToString()); }, () => Number >= 0 ); } } public class DelegateCommand : ICommand { System.Action execute; System.Func<bool> canExecute; public bool CanExecute(object parameter) { return canExecute(); } public event System.EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { execute(); } public DelegateCommand(System.Action execute, System.Func<bool> canExecute) { this.execute = execute; this.canExecute = canExecute; } } }
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:WpfApplication1" Title="MainWindow" Height="150" Width="225"> <Window.Resources> <src:TestViewModel x:Key="viewModel"/> </Window.Resources> <StackPanel DataContext="{StaticResource viewModel}"> <TextBox Text="{Binding Number}"/> <Button Content="sqrt" Command="{Binding SqrtCommand}"/> </StackPanel> </Window>
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="150" Width="225"> <CheckBox> <CheckBox.Style> <Style TargetType="CheckBox"> <Style.Triggers> <Trigger Property="IsChecked" Value="true"> <Setter Property="Content" Value="Checked!!"/> </Trigger> </Style.Triggers> </Style> </CheckBox.Style> </CheckBox> </Window>つまりCheckBoxのIsCheckedがtrueになったとき、CheckBoxのContentを"Checked!!"にする、という意味です。
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:WpfApplication1" Title="MainWindow" Height="150" Width="225"> <Window.Resources> <src:TestViewModel x:Key="viewModel"/> <Style x:Key="style" TargetType="TextBlock"> <Style.Triggers> <DataTrigger Binding="{Binding IsViewModelEnabled}" Value="true"> <Setter Property="Text" Value="Checked!!"/> </DataTrigger> </Style.Triggers> </Style> </Window.Resources> <CheckBox DataContext="{StaticResource viewModel}" IsChecked="{Binding IsViewModelEnabled}" > <TextBlock Style="{StaticResource style}"/> </CheckBox> </Window>
namespace WpfApplication1 { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } public class TestViewModel { public bool IsViewModelEnabled { get; set; } } }