开发者

How to set a property in a trigger (WPF)

I have a user control that has a text box and a button.

I want to disable the text box using trigger ( I know how to do this via code)

The XAML is as follow:

<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.Ro开发者_StackOverflowwDefinitions>
    <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>

The code is:

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;
        }
       }
}

But this setup disable both TextBox and button. How can I only disable the button? If I have several Textbox and I want only some of them are disabled, what is the best option? If I have several different UIElements (such as textbox, calandar, datagrid and .. and I want to disable all of them using one triger, what should I do?


Try moving the style down to your grid, and set the TargetName to textBox1. See the answer to this question for an example: Triggers Based on Properties from DataContext

Btw, you should be able to bind the value of IsEditing directly to textBox1.IsEnabled (warning: coding in-place, so code may not work as-is)

<Button IsEnabled="{Binding Path=IsEditing RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl1}}} />


Set the TargetName of the Setter to the name of the button.


best option is group them and and then disable in one go.

in your code, you havent specified the x:Key for your style, if you dont specify the key, it tries to use it as default style for all controls type of UserControl1. and then you can attach that style to your child controls in your UserControl1:

for Key:

<Style x:Key="styleDefault" TargetType="Control">

attach style to your child controls:

 <Button Style="{StaticResource styleDefault}"></Button>


I think the problem here is that you are using a EventTrigger, it is the only trigger that can be set directly on a style, not using template. AFAIK if you use this kind of trigger you can only set properties of the object that fired the event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜