[PR]
×
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
プログラミング、3DCGとその他いろいろについて
[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
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; } } }
public enum FigureType { Ellipse, Rectangle, Triangle }3つの値はそれぞれ図形のタイプを表しています。
using System.Windows; namespace DataTemplateSelectorDemo { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); figuresView.ItemsSource = System.Enum.GetValues(typeof(FigureType)); } } public enum FigureType { Ellipse, Rectangle, Triangle } }
<Window x:Class="DataTemplateSelectorDemo.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"> <ListBox x:Name="figuresView"/> </Window>
using System.Windows; using System.Windows.Controls; namespace DataTemplateSelectorDemo { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); figuresView.ItemsSource = System.Enum.GetValues(typeof(FigureType)); } } public enum FigureType { Ellipse, Rectangle, Triangle } public class FigureTypeDataTemplateSelector : DataTemplateSelector {
public DataTemplate EllipseDataTemplate { get; set; }
public DataTemplate RectangleDataTemplate { get; set; }
public DataTemplate TriangleDataTemplate { get; set; }
public override DataTemplate SelectTemplate(
object item,
DependencyObject container
)
{
if (!(item is FigureType)) return null;
switch ((FigureType)item)
{
case FigureType.Ellipse:
return EllipseDataTemplate;
case FigureType.Rectangle:
return RectangleDataTemplate;
case FigureType.Triangle:
return TriangleDataTemplate;
default: return null;
}
}
}
}
<Window x:Class="DataTemplateSelectorDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:model="clr-namespace:DataTemplateSelectorDemo" Title="MainWindow" Height="350" Width="525"> <ListBox x:Name="figuresView" ItemTemplateSelector="{DynamicResource templateSelector}"/> <Window.Resources>
<DataTemplate x:Key="MyEllipseDataTemplate">
<Ellipse Stroke="Black" Width="100" Height="100"/>
</DataTemplate>
<DataTemplate x:Key="MyRectangleDataTemplate">
<Rectangle Stroke="Black" Width="150" Height="100"/>
</DataTemplate>
<DataTemplate x:Key="MyTriangleDataTemplate">
<Polygon Stroke="Black" Points="100,0 0,100 200,100"/>
</DataTemplate>
<model:FigureTypeDataTemplateSelector
x:Key="templateSelector"
EllipseDataTemplate="{StaticResource MyEllipseDataTemplate}"
RectangleDataTemplate="{StaticResource MyRectangleDataTemplate}"
TriangleDataTemplate="{StaticResource MyTriangleDataTemplate}"
/> </Window.Resources>
</Window>