Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在触发器(WPF)中设置属性_C#_Wpf_Xaml_Styles - Fatal编程技术网

C# 如何在触发器(WPF)中设置属性

C# 如何在触发器(WPF)中设置属性,c#,wpf,xaml,styles,C#,Wpf,Xaml,Styles,我有一个用户控件,它有一个文本框和一个按钮 我想使用触发器禁用文本框(我知道如何通过代码执行此操作) XAML如下所示: <UserControl x:Class="MyProject.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

我有一个用户控件,它有一个文本框和一个按钮

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

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;
        }
       }
}
使用系统;
使用System.Windows;
使用System.Windows.Controls;
名称空间MyProject
{
/// 
///UserControl1.xaml的交互逻辑
/// 
公共部分类UserControl1:UserControl
{
公共静态只读DependencyProperty IsEditingProperty=DependencyProperty.Register(
“iEdit”、typeof(布尔)、typeof(用户控件)、新属性ymetadata(false));
公共布尔编辑
{
获取{return(Boolean)GetValue(IsEditingProperty);}
set{SetValue(IsEditingProperty,value);}
}
公共用户控制1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
IsEditing=!IsEditing;
}
}
}

但此设置同时禁用文本框和按钮。我怎么能只禁用按钮?如果我有几个文本框,我只希望其中一些被禁用,那么最好的选择是什么?如果我有几个不同的UIElement(如textbox、calandar、datagrid和..),我想用一个触发器禁用所有这些元素,我该怎么办?

Setter
TargetName设置为按钮的名称。

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

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

对于键:

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

将样式附加到子控件:

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

尝试将样式向下移动到网格中,并将TargetName设置为textBox1。有关示例,请参阅此问题的答案:

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


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

我这样做了:但我收到一个错误消息:属性“TargetName”不代表“Setter”的有效目标,因为未找到名为“textBox1”的元素。请确保在使用该目标的任何Setter、触发器或条件之前声明该目标。我将x:key添加到style:中,然后将其与TextBox一起使用,如下所示:但我收到以下错误:“UserControl1”TargetType与元素“TextBox”的类型不匹配。已更新,m我做了这件事,但是触发器不起作用。没有控件被禁用。我正在使用绑定技术。我找不到任何使用触发器的方法。
<Button IsEnabled="{Binding Path=IsEditing RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl1}}} />