dependency-properties 相关问题

WPF和Silverlight中支持数据绑定的属性,包括样式,继承,动画和默认值。它允许您在DependencyObject上设置附加属性。

在UWP App中,使用DependencyProperty,TextBlock的FontSize和FontStyle发生了变化,但FontFamily没有发生变化

使用 UWP 我想打印包含 TextBlock 的报告。根据偏好,我想更改 TextBlock FontSize、FontStyle 和 FontFamily,但 DependencyProperty 会更改 FontSize 和

回答 1 投票 0

如何使用 EF 存储/映射复杂数据类型?

CategoryModel 具有对象画笔类型(复杂数据类型)的属性和依赖属性颜色,无法直接使用 EF 存储,因此我使用序列化并将其存储为字符串。 使用系统...

回答 1 投票 0

从自定义 UserControl 的 DependencyProperty 返回值并使用 viewModel 的正确方法?

我将自定义用户控件包装到 dll,并希望引用它的其他人能够通过绑定到“Result”属性来获取或设置结果值。 这是 CustomControl.cs 代码: 公开部分

回答 1 投票 0

将属性值从父级用户控件传递到子级的 DependencyProperty

如何将属性(SomeProperty)从ParentUserControl上下文传递到ChildUserControl的DependencyProperty(MyDProperty)? 在 XAML 中,它应该是: 如何将 ParentUserControl 上下文中的 property (SomeProperty) 传递到 ChildUserControl 的 DependencyProperty (MyDProperty)? 在XAML中,应该是: 但是,由于某种原因,MyDProperty 永远不会使用 Parent.DataContext.SomeProperty 设置。 就我而言,我正在传递一个操作,但这并不重要。我认为问题出在绑定上。 家长用户控制: public Action RemoveEsl1 => throw new NotImplementedException(); <uc:ChildUserControl Title="ESL 1" RemoveEslAction="{Binding RemoveEsl1}" DataContext="{Binding Esl1}"/> 子用户控件: public static readonly DependencyProperty RemoveEslActionProperty = DependencyProperty.Register(nameof(RemoveEslAction), typeof(Action), typeof(ChildUserControl), new PropertyMetadata(delegate { })); public Action RemoveEslAction { get => (Action)GetValue(RemoveEslActionProperty); set => SetValue(RemoveEslActionProperty, value); } 我在这里找到了各种技巧,但没有一个适合我或有效。 回答我自己的问题(检查 ParentUserControl): 型号: public class RootModel : ViewModelBase { private ParentModel parentModel = new(); public ParentModel ParentModel { get => parentModel; set => RisePropertyChanged(ref parentModel, value); } } public class ParentModel : ViewModelBase { private ChildModel childModel = new(); public ChildModel ChildModel { get => childModel; set => RisePropertyChanged(ref childModel, value); } public string ParentModelProperty => "Correct value from ParentModel"; } public class ChildModel : ViewModelBase { private string childModelProperty = "Wrong default value from ChildModel"; public string ChildModelProperty { get => childModelProperty; set => RisePropertyChanged(ref childModelProperty, value); } } 主窗口: <Window.DataContext> <model:RootModel/> </Window.DataContext> <uc:ParentUserControl DataContext="{Binding ParentModel}"/> 家长用户控件: <uc:ChildUserControl ChildDependency="{Binding DataContext.ParentModelProperty, RelativeSource={RelativeSource AncestorType=UserControl}}" DataContext="{Binding ChildModel}"/> 子用户控件: <StackPanel> <Label Content="Dependency property:"/> <Label Content="{Binding ChildDependency, RelativeSource={RelativeSource AncestorType=UserControl}}"/> <Separator/> <Label Content="Property:"/> <Label Content="{Binding ChildModelProperty}"/> </StackPanel> public partial class ChildUserControl : UserControl { public static readonly DependencyProperty ChildDependencyProperty = DependencyProperty.Register(nameof(ChildDependency), typeof(string), typeof(ChildUserControl), new ("Wrong default DP value from ChildUserControl")); public string ChildDependency { get => (string)GetValue(ChildDependencyProperty); set => SetValue(ChildDependencyProperty, value); } public ChildUserControl() { InitializeComponent(); } } 这就是如何将属性 (SomeProperty) 从 ParentUserControl 上下文传递到 ChildUserControl 的 DependencyProperty (MyDProperty)。

回答 1 投票 0

WPF UserControl 无法与 DependencyProperty OnSomeBytePropertyChanged 一起使用

首先,我在 WPF 项目中创建了一个 UserControl,用于在设置窗口中设置颜色。 该窗口有一个名为 MainWindowViewModel.cs 的 ViewModel,其中包含: 使用 System.Collections.ObjectMo...

回答 1 投票 0

WPF 将页面分配给依赖属性

TL;DR:将 {StaticResource SomePage} 绑定到 Page 类型的 DependencyProperty 会出现错误 Microsoft.VisualStudio.XSurface.Wpf.Page 不是属性的有效值... 长版 我正在尝试...

回答 1 投票 0

如何将自定义 UserControl 的依赖属性双向绑定到另一个 UserControl 的 DataContext 的属性?

我有一个用户控件,其代码隐藏已声明依赖属性。我还有另一个 UserControl 作为主 UI。 主 UI 用户控件已将其 DataContext 设置为 viewmo...

回答 1 投票 0

有没有办法可以使用这个依赖属性? WPF C#

我试图在自定义控件中设置 SKQuantitySpinEdit 父类的增量属性,但无法这样做! ControlTemplate spinOptLegQuantityTemplate = (ControlTemplate)(grdMultiF...

回答 1 投票 0

MVVM、XAML、C# - DependencyProperty - ItemsControl 中的 ObservableCollection 无法识别 Property

我对 MVVM 和 c# 系统相当陌生,只是将其用作一个小小的业余爱好。在过去的三天里,我一直对以下问题感到绝望。 我有一个带有相应的自定义控件视图...

回答 2 投票 0

无法实例化[org.flywaydb.core.Flyway]:工厂方法“flyway”抛出异常并显示消息:名称不能为空

spring从2.7.x升级到3.2.1版本后 并将 Flyway 从 8.5 升级到版本 10.4.1 时,我收到了如标题所示的异常: 无法实例化 [org.flywaydb.core.Flyway]:工厂

回答 1 投票 0

WPF 绑定到 UserControl 的 DependencyProperty 未按预期工作[重复]

我在 DependecyProperty 绑定方面遇到一些奇怪的问题。 为了使问题更简单,我创建了一些虚拟控件,它具有相同的不需要的行为 我有一个用户控件,它有一个

回答 1 投票 0

WPF 绑定到自定义控件依赖属性不会触发属性更改

我在绑定到自定义控件上的依赖属性时遇到问题。 这是我的自定义控件。它基本上是一个文本框,只允许数字输入并公开“值”依赖项

回答 1 投票 0

ObservableCollection 绑定到另一个 ObservableCollection 不会触发 CollectionChanged

我有 ControlA 托管 ControlB,它们都有 ObservableCollection 类型的 DependencyProperty,嵌套控件的 (ControlB) 属性绑定到父级 (ControlA) 属性,如果有的话

回答 1 投票 0

如何为自定义WPF控件添加最小和最大验证?

我想创建一个具有多个 WPF 依赖属性的自定义控件。 属性之一是返回 Thickness 对象。 自定义控件应该适合设计使用...

回答 1 投票 0

依赖属性收到xaml更改时的回调

当我在运行时设置 IsClosed 的值时,OnIsClosedChanged() 会被调用。 但是,设计器设置属性的值,但不会调用 OnIsClosedChanged()。 公共静态

回答 2 投票 0

在 ControlTemplate 中绑定附加属性

<Setter Property="Height" Value="22"/> <Setter Property="Padding" </desc> <question vote="0"> <pre><code>&lt;Style x:Key=&#34;DeckButton_Help&#34; TargetType=&#34;{x:Type Button}&#34;&gt; &lt;Setter Property=&#34;Height&#34; Value=&#34;22&#34;/&gt; &lt;Setter Property=&#34;Padding&#34; Value=&#34;0,0&#34;/&gt; &lt;Setter Property=&#34;Margin&#34; Value=&#34;0,0&#34;/&gt; &lt;Setter Property=&#34;Content&#34; Value=&#34;&#34;/&gt; &lt;Setter Property=&#34;local:ButtonProperties.ImageSource&#34; Value=&#34;&#34;/&gt; &lt;Setter Property=&#34;Template&#34;&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType=&#34;{x:Type Button}&#34;&gt; &lt;Button Style=&#34;{StaticResource ButtonForeground}&#34; HorizontalContentAlignment=&#34;Left&#34; Background=&#34;#e1e1e1&#34; BorderBrush=&#34;#9d9d9d&#34; Command=&#34;{TemplateBinding Command}&#34; Padding=&#34;5,4,0,0&#34; &gt; &lt;Button.Content&gt; &lt;Grid Height=&#34;16&#34;&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width=&#34;16&#34;/&gt; &lt;ColumnDefinition Width=&#34;*&#34;/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Image Style=&#34;{StaticResource ButtonImage}&#34; Height=&#34;16&#34; Width=&#34;16&#34; Source=&#34;{TemplateBinding local:ButtonProperties.ImageSource}&#34; VerticalAlignment=&#34;Center&#34; HorizontalAlignment=&#34;Left&#34; Margin=&#34;0,-4,0,0&#34;/&gt; &lt;TextBlock Grid.Column=&#34;1&#34; Text=&#34;{TemplateBinding Content}&#34; HorizontalAlignment=&#34;Center&#34; Margin=&#34;5,0,0,0&#34; /&gt; &lt;/Grid&gt; &lt;/Button.Content&gt; &lt;/Button&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>这里我尝试从外部按钮设置 local:ButtonProperties.ImageSource 属性。</p> <pre><code>&lt;Button local:ButtonProperties.ImageSource=&#34;/CommonResources;component/bitmap/IDB_BUTTON_HELP.PNG&#34; Style=&#34;{StaticResource DeckButton_Help}&#34; Command=&#34;{Binding HelpCommand}&#34; Content=&#34;Help&#34; Margin=&#34;20,0,0,0&#34; Width=&#34;65&#34; /&gt; </code></pre> <p>而 ButtonProperties 是这样定义的:</p> <pre><code>public class ButtonProperties { public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.RegisterAttached( &#34;ImageSource&#34;, typeof(string), typeof(ButtonProperties), new FrameworkPropertyMetadata(null)); public static string GetImageSource(DependencyObject obj) { return (string)obj.GetValue(ImageSourceProperty); } public static void SetImageSource(DependencyObject obj, string value) { obj.SetValue(ImageSourceProperty, value); } } </code></pre> <p>问题是图标没有设置,当我调试时,SetImageSource和GetImageSource没有被调用。</p> </question> <answer tick="true" vote="1"> <p>您可以使用常规 Binding 来代替 TemplateBinding,如下所示。它将提供从字符串到 ImageSource 的自动类型转换。</p> <pre><code>&lt;Image ... Source=&#34;{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ButtonProperties.ImageSource)}&#34;/&gt; </code></pre> <p>如果您正确设置图像路径,这应该可以工作(我已经在我的设备上测试了代码)。</p> <hr/> <p>然而,将附加属性声明为 <pre><code>ImageSource</code></pre> 而不是 <pre><code>string</code></pre> 是有意义的,这样 TemplateBinding 也可以工作。当您在 Button 上分配附加属性时,就会发生从字符串到 ImageSource 的转换,并且附加属性不能包含任何无效字符串。</p> <pre><code>public class ButtonProperties { public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.RegisterAttached( &#34;ImageSource&#34;, typeof(ImageSource), typeof(ButtonProperties)); public static ImageSource GetImageSource(DependencyObject obj) { return (ImageSource)obj.GetValue(ImageSourceProperty); } public static void SetImageSource(DependencyObject obj, ImageSource value) { obj.SetValue(ImageSourceProperty, value); } } </code></pre> <hr/> <p>旁注:</p> <ol> <li>在某些情况下,依赖属性 setter 和 getter 中的断点不会被击中,例如当在 XAML 中设置属性时。框架直接调用SetValue和GetValue。</li> <li>如果<pre><code>string</code></pre>类型的属性的默认值为null,则不需要在属性定义中使用<pre><code>new FrameworkPropertyMetadata(null)</code></pre>,也不需要在样式定义中使用<pre><code>&lt;Setter Property=&#34;local:ButtonProperties.ImageSource&#34; Value=&#34;&#34;/&gt;</code></pre>。</li> </ol> </answer> <answer tick="false" vote="0"> <p>TemplateBinding 不会执行自动类型转换,正如您在 Binding 中所期望的那样</p> <pre><code>Source=&#34;{TemplateBinding local:ButtonProperties.ImageSource} </code></pre> <p>其中源属性的类型为 <pre><code>string</code></pre>,目标属性的类型为 <pre><code>ImageSource</code></pre>。</p> <p>您必须在附加属性的声明中使用正确的类型:</p> <pre><code>public class ButtonProperties { public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.RegisterAttached( &#34;ImageSource&#34;, typeof(ImageSource), typeof(ButtonProperties)); public static ImageSource GetImageSource(DependencyObject obj) { return (ImageSource)obj.GetValue(ImageSourceProperty); } public static void SetImageSource(DependencyObject obj, ImageSource value) { obj.SetValue(ImageSourceProperty, value); } } </code></pre> <hr/> <p>您还必须删除 Setter </p> <pre><code>&lt;Setter Property=&#34;local:ButtonProperties.ImageSource&#34; Value=&#34;&#34;/&gt; </code></pre> <p>来自按钮样式,因为空字符串对于 ImageSource 类型的属性来说不是有效值。</p> <hr/> <p>请注意,当在 XAML 中设置 <pre><code>SetImageSource</code></pre> 属性时,不会调用 <pre><code>ImageSource</code></pre> 方法。框架绕过setter和getter,直接调用<pre><code>SetValue</code></pre>和<pre><code>GetValue</code></pre>。</p> </answer> </body></html>

回答 0 投票 0

绑定Button的ImageSource依赖属性

<Setter Property="Height" Value="22"/> <Setter Property="Padding" </desc> <question vote="0"> <pre><code>&lt;Style x:Key=&#34;DeckButton_Help&#34; TargetType=&#34;{x:Type Button}&#34;&gt; &lt;Setter Property=&#34;Height&#34; Value=&#34;22&#34;/&gt; &lt;Setter Property=&#34;Padding&#34; Value=&#34;0,0&#34;/&gt; &lt;Setter Property=&#34;Margin&#34; Value=&#34;0,0&#34;/&gt; &lt;Setter Property=&#34;Content&#34; Value=&#34;&#34;/&gt; &lt;Setter Property=&#34;local:ButtonProperties.ImageSource&#34; Value=&#34;&#34;/&gt; &lt;Setter Property=&#34;Template&#34;&gt; &lt;Setter.Value&gt; &lt;ControlTemplate TargetType=&#34;{x:Type Button}&#34;&gt; &lt;Button Style=&#34;{StaticResource ButtonForeground}&#34; HorizontalContentAlignment=&#34;Left&#34; Background=&#34;#e1e1e1&#34; BorderBrush=&#34;#9d9d9d&#34; Command=&#34;{TemplateBinding Command}&#34; Padding=&#34;5,4,0,0&#34; &gt; &lt;Button.Content&gt; &lt;Grid Height=&#34;16&#34;&gt; &lt;Grid.ColumnDefinitions&gt; &lt;ColumnDefinition Width=&#34;16&#34;/&gt; &lt;ColumnDefinition Width=&#34;*&#34;/&gt; &lt;/Grid.ColumnDefinitions&gt; &lt;Image Style=&#34;{StaticResource ButtonImage}&#34; Height=&#34;16&#34; Width=&#34;16&#34; Source=&#34;{TemplateBinding local:ButtonProperties.ImageSource}&#34; VerticalAlignment=&#34;Center&#34; HorizontalAlignment=&#34;Left&#34; Margin=&#34;0,-4,0,0&#34;/&gt; &lt;TextBlock Grid.Column=&#34;1&#34; Text=&#34;{TemplateBinding Content}&#34; HorizontalAlignment=&#34;Center&#34; Margin=&#34;5,0,0,0&#34; /&gt; &lt;/Grid&gt; &lt;/Button.Content&gt; &lt;/Button&gt; &lt;/ControlTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; </code></pre> <p>这里我尝试从外部按钮设置 local:ButtonProperties.ImageSource 属性。</p> <pre><code>&lt;Button local:ButtonProperties.ImageSource=&#34;/CommonResources;component/bitmap/IDB_BUTTON_HELP.PNG&#34; Style=&#34;{StaticResource DeckButton_Help}&#34; Command=&#34;{Binding HelpCommand}&#34; Content=&#34;Help&#34; Margin=&#34;20,0,0,0&#34; Width=&#34;65&#34; /&gt; </code></pre> <p>而 ButtonProperties 是这样定义的:</p> <pre><code>public class ButtonProperties { public static readonly DependencyProperty ImageSourceProperty = DependencyProperty.RegisterAttached( &#34;ImageSource&#34;, typeof(string), typeof(ButtonProperties), new FrameworkPropertyMetadata(null)); public static string GetImageSource(DependencyObject obj) { return (string)obj.GetValue(ImageSourceProperty); } public static void SetImageSource(DependencyObject obj, string value) { obj.SetValue(ImageSourceProperty, value); } } </code></pre> <p>问题是图标没有设置,当我调试时,SetImageSource和GetImageSource没有被调用。</p> </question> <answer tick="false" vote="0"> <p>附加属性应该像这样使用</p> <pre><code>&lt;Image Style=&#34;{StaticResource ButtonImage}&#34; Height=&#34;16&#34; Width=&#34;16&#34; Source=&#34;{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:ButtonProperties.ImageSource)}&#34; VerticalAlignment=&#34;Center&#34; HorizontalAlignment=&#34;Left&#34; Margin=&#34;0,-4,0,0&#34;/&gt; </code></pre> <p>如果您正确设置图像路径,这应该可以工作(我已经在我的设备上测试了代码)。</p> </answer> </body></html>

回答 0 投票 0

为什么ItemsControl中的所有TextBox都使用最后动态添加的ValidationRule?

我有一个自定义文本框,在设置 Min 和 Max 属性时添加 MinMaxValidationRule 公共类 TextBox2 :文本框 { 公共静态只读 DependencyProperty MinProperty =

回答 1 投票 0

用户控件内复选框的 IsChecked 绑定不适合

在UserControl.xaml中: 在 UserControl.xaml 中: <UserControl x:Class="OurNameSpace.SystemCalibration.Controls.PortSelector" 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:local="clr-namespace:OurNamespace.SystemCalibration.Controls" xmlns:converters="clr-namespace:OurNamespace.SystemCalibration.Converters" xmlns:sys="clr-namespace:System;assembly=mscorlib" x:Name="portSelector" mc:Ignorable="d"> <Grid ….. <StackPannel …. <CheckBox Content="Port 4" Margin="0 5 0 5" IsChecked="{Binding cb4_IsChecked, ElementName=portSelector, UpdateSourceTrigger=PropertyChanged}"> <CheckBox.Visibility> <MultiBinding Converter="{StaticResource maxPortsToVisibilityConverter}"> <Binding Path="MaxPorts" ElementName="portSelector"/> <Binding Source="{StaticResource port4}"/> </MultiBinding> </CheckBox.Visibility> </CheckBox> </StackPanel> </Grid> 在 usercontrol.xaml.cs 中: public partial class PortSelector : UserControl { public static readonly DependencyProperty cb4_isCheckedProperty = DependencyProperty.Register(nameof(cb4_IsChecked), typeof(bool), typeof(PortSelector)); public bool cb4_IsChecked { get => (bool)GetValue(cb4_isCheckedProperty); set { SetValue(cb4_isCheckedProperty, value); UpdatedSelectedPortsString(); } } } 如果我在调用“UpdatedSelectedPortsString”的线路上放置一个中断,则断点永远不会被命中。 此用户控件托管在 TabControl 内的另一个用户控件内。当我单击该复选框时,我希望执行依赖属性的 Set 部分,但该代码似乎根本没有被调用。我什至尝试向 IsChecked 绑定添加一个假值转换器,并且转换器内的代码确实执行,但设置器不执行。如果我不绑定 IsChecked,而是添加具有依赖属性的 Command 绑定,并将命令处理程序放在同一 usercontrol.xaml.cs 代码隐藏部分类中,它也可以工作。唯一似乎不起作用的是直接绑定 IsChecked。 您必须使用 PropertyChangedCallback 委托,如下所示: public static readonly DependencyProperty cb4_IsCheckedProperty = DependencyProperty.Register(nameof(cb4_IsChecked), typeof(bool), typeof(PortSelector), new PropertyMetadata(false, cb4_IsCheckedChangedCallback)); public bool cb4_IsChecked { get { return (bool)GetValue(cb4_IsCheckedProperty); } set { SetValue(cb4_IsCheckedProperty, value); } } private static void cb4_IsCheckedChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) { PortSelector uc = d as PortSelector; uc.UpdatedSelectedPortsString(); } 每次 IsChacked 更改时都会调用“cb4_IsCheckedChangedCallback”方法。 如果这不起作用。此代码应该用于绑定: IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:PortSelector}}, Path=cb4_IsChecked}"

回答 1 投票 0

WPF 自定义控件绑定 - 如何确保呈现您的值

我仅用代码创建了一个自定义控件,我希望它的占用空间尽可能小,因为它将根据样式模板内网格中的值进行重复。目前它工作一些...

回答 0 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.