How to set a property in a trigger (WPF)?

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.

Up vote 3 down vote favorite share g+ share fb share tw.

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: The code is: using System; using System. Windows; using System.Windows.

Controls; namespace MyProject { /// /// Interaction logic for UserControl1. Xaml /// 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? C# wpf xaml styles link|improve this question edited Jul 4 '11 at 18:37Sung5,19412876 asked Jul 4 '11 at 16:39user654019403110 71% accept rate.

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).

I am usning binding technique. I could not find any way to use triggers. – user654019 Jul 5 '11 at 14:33 Binding is cleaner anyway, so that's good.

– Kshitij Mehta Jul 5 '11 at 16:35.

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

I did this: But I am getting an error saying: The property 'TargetName' does not represent a valid target for the 'Setter' because an element named 'textBox1' was not found. Make sure that the target is declared before any Setters, Triggers or Conditions that use it. – user654019 Jul 4 '11 at 16:44.

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 don't 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: attach style to your child controls.

I add x:key to style: and then used it with TextBox as follow: But I am getting this error: 'UserControl1' TargetType does not match type of element 'TextBox'. – user654019 Jul 4 '11 at 17:09 updated, made it generic so that it can be used for all Controls – anvarbek raupov Jul 4 '11 at 17:11 I did this but then the triger doesn't work. No control became disable.

– user654019 Jul 4 '11 at 17:26.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions