- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个用户控件,其中我有几个文本框/日历/...控件,它们都应该根据我的用户控件 (IsEditing) 中的属性启用或禁用
我在我的用户控件中创建了一个依赖属性 IsEditing,现在我想定义一个样式,所有控件都使用它来启用或禁用。
我正在尝试做这样的事情:
<ad:DocumentContent.Resources>
<Style x:Key="ReadOnlyMode">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="Visibility" Value="Visible"/>
<Setter Property="IsEnabled" Value="False" />
<Style.Triggers>
<Trigger Property="IsEditing" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
</ad:DocumentContent.Resources>
但我收到此错误:在类型“FrameworkElement”中找不到属性“IsEditing”。
如何为触发器找到正确的属性?
更新 1:
我将 IsEditing 定义如下:
public static readonly DependencyProperty IsEditingProperty = DependencyProperty.Register(
"IsEditing", typeof(Boolean), typeof(MyDetailDataControl), new PropertyMetadata(false));
定义样式如下:
<ad:DocumentContent.Resources>
<Style x:Key="ReadOnlyMode" xmlns:u="clr-namespace:MyProject.Controls" TargetType="{x:Type u:MyDetailDataControl}">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="Visibility" Value="Visible"/>
<Setter Property="IsEnabled" Value="False" />
<Style.Triggers>
<Trigger Property="IsEditing" Value="True">
<Setter Property="Background" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
</ad:DocumentContent.Resources>
但我现在收到以下错误:在类型“MyDetailDataControl”中找不到属性“IsEditing”。
我认为依赖属性定义有问题。
更新 2:
我按如下方式更改了 xaml(在用户控制部分添加 xmlns:l="clr-namespace:MyProject.Controls"以便命名空间随处可用)并且还使用了此处定义的方法 http://www.wpfmentor.com/2009/01/how-to-debug-triggers-using-trigger.html调试触发器)
<ad:DocumentContent.Resources>
<Style x:Key="ReadOnlyMode" TargetType="TextBox">
<Style.Triggers>
<Trigger my:TriggerTracing.TriggerName="BoldWhenMouseIsOver" my:TriggerTracing.TraceEnabled="True" Property="l:MyDetailDataControl.IsEditing" Value="True">
<Setter Property="TextBox.Background" Value="Green"/>
</Trigger>
</Style.Triggers>
</Style>
</ad:DocumentContent.Resources>
但是触发器没有被触发?你我需要实现通知属性机制吗?如果是,我该怎么做?当属性是附加属性时,是否有任何特殊技术来实现属性更改通知?
更新 3:改成这个版本,和我自己的IsEditing属性没有关系。但还是不行。
<ad:DocumentContent.Resources>
<Style x:Key="MyStyle" TargetType="{x:Type l:MyDetailDataControl}" >
<Style.Triggers>
<Trigger my:TriggerTracing.TriggerName="BoldWhenMouseIsOver" my:TriggerTracing.TraceEnabled="True" Property="IsMouseOver" Value="True">
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</Style.Triggers>
</Style>
</ad:DocumentContent.Resources>
最佳答案
您需要设置 TargetType
样式到您的 UserControl 的类型。或者你用它作为所有属性的前缀。
<!-- Using TargetType -->
<Style xmlns:u="clr-namespace:Test.UserControls"
x:Key="ReadOnlyMode"
TargetType="{x:Type u:MyUserControl1}">
<Style.Triggers>
<Trigger Property="IsEditing" Value="True">
<!-- ... -->
</Trigger>
</Style.Triggers>
</Style>
<!-- Using qualified property -->
<Style xmlns:u="clr-namespace:Test.UserControls"
x:Key="ReadOnlyMode">
<Style.Triggers>
<Trigger Property="u:MyUserControl1.IsEditing" Value="True">
<!-- ... -->
</Trigger>
</Style.Triggers>
</Style>
关于您的其他问题:该属性需要一个 CLR-Wrapper:
public static readonly DependencyProperty IsEditingProperty =
DependencyProperty.Register("IsEditing", typeof(Boolean), typeof(MyDetailDataControl), new UIPropertyMetadata(false));
// This:
public Boolean IsEditing
{
get { return (Boolean)GetValue(IsEditingProperty); }
set { SetValue(IsEditingProperty, value); }
}
关于c# - 如何在用户控件中以样式定义触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6571349/
概述 触发器是 MySQL 的数据库对象之一,不需要程序调用或手工启动,而是由事件来触发、激活,从而实现执行,包括 INSERT 语句、UPDATE 语句和 DELETE 语句 创建触发器 1. 创建
当我为单元格获得的值是某种类型时,我试图设置一个触发器来显示文本块。 我已经成功地设法在相同的情况下显示图像,但在这种情况下我不需要图像,而是一些文本。 已注释掉行以进行测试。尝试使其工作。注释掉的代
我需要在 phpmyadmin 中为 2 个表创建一个触发器。 所以有表 stores 和 tbl_storefinder_stores。 我想从 stores 表中插入 4 个东西(名称、地址、经度
阅读目录 1、触发器 2、触发器类型 3、触发器语法 4、插入数据触发器案例 5、修改数据触发器案例
SQLite 触发器(Trigger) SQLite 的触发器是数据库的回调函数,它会在指定的数据库事件发生时自动执行/调用。以下是关于SQLite的触发器的要点:SQLite **触发器(Trig
请帮我写一个向表中添加新行的触发器。 我的数据库中有 3 个表: 地区(id,名字); id - 主要; 技术人员(身份证、姓名); id - 主要; 可用性(id、区域、技术、计数); id - p
我正在编写一个触发器来审核表中的更新和删除。我正在使用 SQL Server 2008 我的问题是, 有没有办法在不经过删除和插入表的选择阶段的情况下找出对记录采取的操作? 另一个问题是,如果记录被删
我的表: TableA (id number, state number) TableB (id number, tableAId number, state number) TableC (id n
我很少写触发器。我可以帮助设置这件事。 CREATE TRIGGER audit_tableName ON dbo.tableNameAudit AFTER CREATE, UPDATE, DELET
我之前从未在 Oracle 中创建过触发器,所以我正在寻找一些方向。 如果 ID 不在插入语句中,我想创建一个将 ID 增加 1 的触发器。 ID 应该从 10000 开始,当插入一条记录时,下一个
考虑以下两个(假设的)表 温度 * day * time * lake_name * station * temperature_f 温度_总结 * day * lake_name * station
如何在 SQL 触发器中获取更新记录的值 - 如下所示: CREATE TRIGGER TR_UpdateNew ON Users AFTER UPDATE AS BEGIN S
我是 Cassandra 新手,使用 Cassandra 3.10 并有类似的表格 create table db1.table1 (id text, trip_id text, event_time
在 MSSQL 中执行 TRUNCATE(而不是删除)时如何触发触发器 最佳答案 来自msdn : TRUNCATE TABLE cannot activate a trigger because t
我正在尝试在 sql developer 中创建一个简单的触发器,以在工资发生变化时显示工资的变化 CREATE OR REPLACE TRIGGER salary_changes BEFORE DE
我有三个表: Table1: Customers (CustomerId, Name, CustomerAddress) Table2: AccountManagers(ManagerId, Name
在 Sql Server 2005 触发器中有没有办法在执行期间获取触发器附加到的表的名称和架构? 最佳答案 SELECT OBJECT_NAME(parent_id) AS [Table],
使用 MySQL 5.5,以下触发器因错误而被拒绝: create trigger nodups before insert on `category-category` for each row b
我使用 fancybox 打开一个带有表单的弹出窗口。目前,当鼠标离开主页时,弹出窗口就会出现。为了完成这项工作,我有一个隐藏的链接标签,我用trigger()函数模拟它,单击该函数,以便该链接的hr
我的触发器触发 INSERT, UPDATE and DELETE .我需要根据触发触发器的操作从适当的内存表( inserted, deleted )插入。由于只有 inserted位于 INSER
我是一名优秀的程序员,十分优秀!