【问题标题】:How to set a property in a trigger (WPF)如何在触发器中设置属性(WPF)
【发布时间】:2011-07-04 16:39:23
【问题描述】:

我有一个包含文本框和按钮的用户控件。

我想使用触发器禁用文本框(我知道如何通过代码执行此操作)

XAML 如下:

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

但是这个设置禁用了文本框和按钮。我怎样才能只禁用按钮?如果我有几个文本框并且我只想禁用其中的一些,那么最好的选择是什么?如果我有几个不同的 UIElement(例如 textbox、calandar、datagrid 和 ..,并且我想使用一个触发器来禁用所有这些 UIElement,我该怎么办?

【问题讨论】:

    标签: c# wpf xaml styles


    【解决方案1】:

    尝试将样式向下移动到网格中,并将 TargetName 设置为 textBox1。例如,请参阅此问题的答案:Triggers Based on Properties from DataContext

    顺便说一句,您应该能够将 IsEditing 的值直接绑定到 textBox1.IsEnabled(警告:就地编码,因此代码可能无法按原样工作)

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

    【讨论】:

    • 我正在使用绑定技术。我找不到任何使用触发器的方法。
    【解决方案2】:

    SetterTargetName 设置为按钮的名称。

    【讨论】:

    • 我这样做了: 但我收到一条错误消息:属性 'TargetName' 不代表'Setter' 的有效目标,因为找不到名为 'textBox1' 的元素。确保在使用它的任何设置器、触发器或条件之前声明目标。
    【解决方案3】:

    最好的选择是将它们分组,然后一次性禁用。

    在您的代码中,您没有为您的样式指定 x:Key,如果您没有指定该键,它会尝试将其用作 UserControl1 的所有控件类型的默认样式。然后您可以将该样式附加到 UserControl1 中的子控件:

    钥匙:

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

    将样式附加到您的子控件:

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

    【讨论】:

    • 我将 x:key 添加到样式中:
    • 已更新,使其通用,以便可用于所有控件
    • 我这样做了,但是触发器不起作用。没有控件被禁用。
    【解决方案4】:

    我认为这里的问题是您使用的是 EventTrigger,它是唯一可以直接在样式上设置的触发器,而不是使用模板。 AFAIK,如果您使用这种触发器,您只能设置触发事件的对象的属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 2013-04-09
      • 2013-08-30
      • 2013-02-16
      • 2022-01-03
      • 2011-08-04
      相关资源
      最近更新 更多