テキストボックスとボタンを持つユーザーコントロールがあります。トリガー(WPF)でプロパティを設定する方法
<UserControl x:Class="MyProject.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:l="clr-namespace:MyProject"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<Style TargetType="l:UserControl1" >
<Style.Triggers>
<Trigger Property="l:UserControl1.IsEditing" Value="True">
<Setter Property="IsEnabled" Value="False"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Button Content="Button" HorizontalAlignment="Left" x:Name="button1" VerticalAlignment="Top" Width="75" Grid.Row="0" Click="button1_Click" />
<TextBox Height="23" HorizontalAlignment="Left" x:Name="textBox1" VerticalAlignment="Top" Width="120" Grid.Row="1"/>
</Grid>
</UserControl>
コードは次のとおりです:
using System;
using System.Windows;
using System.Windows.Controls;
namespace MyProject
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
public static readonly DependencyProperty IsEditingProperty = DependencyProperty.Register(
"IsEditing", typeof(Boolean), typeof(UserControl), new PropertyMetadata(false));
public Boolean IsEditing
{
get { return (Boolean)GetValue(IsEditingProperty); }
set { SetValue(IsEditingProperty, value); }
}
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
IsEditing = !IsEditing;
}
}
}
は私が
XAMLは以下の通りである(私はコードを経由してこれを行う方法を知っている)トリガを使用して、テキストボックスを無効にしたいです
しかし、この設定はTextBoxとボタンの両方を無効にします。どうすればボタンを無効にすることができますか?私がいくつかのテキストボックスを持っていて、そのうちのいくつかだけを無効にしたい場合は、何が最善のオプションですか? 1つのトリガーを使用して、テキストボックス、カレンダー、データグリッドなどのUIElementsが複数ある場合、どのようにすればいいですか?
私はバインディング技術を惜しんでいます。私はトリガーを使用する方法を見つけることができませんでした。 – mans
バインディングはとにかくきれいだからいいですね。 –