标签: dependency-properties

是否存在依赖属性更改时的通知机制?

在Silverlight应用程序中,我试图找出usercontrol上的属性何时发生了变化.我对一个特定的DependencyProperty很感兴趣,但不幸的是控件本身并没有实现INotifyPropertyChanged.

有没有其他方法可以确定价值是否已经改变?

.net silverlight dependency-properties silverlight-2.0

2
推荐指数
1
解决办法
2552
查看次数

System.Windows.DependencyProperty与System.Workflow.ComponentModel.DependencyProperty

.NET Framework似乎有两个DependencyProperty对象的实现

  1. System.Windows.DependencyProperty
  2. System.Workflow.ComponentModel.DependencyProperty

我理解第一个的正常使用是在WPF中,第二个的正常使用是在WF中但是它们之间有什么区别?
如果我没有使用WPF/WF并且仍然想使用最好使用的DependencyProperty?
有没有计划在未来合并?

.net dependency-properties

2
推荐指数
1
解决办法
1067
查看次数

Silverlight 2.0:在更改之前确定是否设置了依赖项属性

我在自定义控件上有一个依赖属性(Foreground),它继承自Control.当我加载控件时,我想查看用户是否已设置依赖项属性,或者在我为其设置值之前是否使用其默认值.这个问题有点复杂,这里是:

该控件位于用户添加到其项目中的自定义控件库中.当他们设置项目时,我可以让他们使用给定的x:key在app.xaml文件中添加一行,其中包含我在设置控件的默认值时读取的设置.但是,如果他们在代码或xaml中设置值,我不想使用此全局值设置值.以下是一些有助于想象我正在谈论的内容的案例:

  1. 用户从我的库中创建一个控件并将其放在他们的页面上.然后他们将前景设置为绿色.我希望该控件的前景值为绿色.

  2. 用户在app.xaml中添加一条特殊行,表示他们希望特定类型的所有控件都具有红色前景.我希望这种控件的前景值为红色.

  3. 用户在app.xaml中添加一条特殊行,表示他们希望特定类型的所有控件都具有红色前景.然后,他们将单个控件(在xaml中)的前景值设置为黄色.我希望那种类型的所有控件的前景值都是红色的,除了它们另外指定为黄色的那个.

加载控件时,我想读取依赖项属性以查看它们是否已设置值,或者它是否是样式中定义的默认值.如果它是默认值,我想从app.xaml文件中读取它们的特殊行,并将前景设置为该颜色.

user-controls controls dependency-properties silverlight-2.0 control-library

2
推荐指数
1
解决办法
1646
查看次数

自定义控件的 WPF 依赖属性

我对如何为自定义控件设置依赖属性有点困惑。

我创建了自定义控件,因此它派生自 Control 类。

public class CustControl : Control 
    {
      static CustControl()
       {
         DefaultStyleKeyProperty.OverrideMetadata(typeof(CustControl), new FrameworkPropertyMetadata(typeof(CustControl)));       
       }        
    }
Run Code Online (Sandbox Code Playgroud)

为了设置依赖属性,我必须在必须从 DependencyObject 派生的类中注册它。所以它应该是另一个类:

class CustClass : DependencyObject
{
    public readonly static DependencyProperty MyFirstProperty = DependencyProperty.Register("MyFirst", typeof(string), typeof(CustControl), new PropertyMetadata(""));

    public string MyFirst
    {
        get { return (string)GetValue(MyFirstProperty); }
        set { SetValue(MyFirstProperty, value); }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在如何将 MyFirst 属性设置为 CustControl 的依赖属性?

c# wpf dependency-properties custom-controls

2
推荐指数
1
解决办法
6684
查看次数

如何实现内部内容依赖属性?

我正在尝试使用依赖项属性实现usercontrol.这是我的问题; 我想设置一个依赖属性与布局子或我的用户控件的子项.它有可能,怎么做?

    <custom:myControl1>
        <Label>Controls</Label>
        <Label>I want</Label>
        <Label>to set</Label>
        <Label>as the dependency property</Label>
        <Button Content="Is it possible?" />
    </custom:myControl1>
Run Code Online (Sandbox Code Playgroud)

wpf dependency-properties

2
推荐指数
1
解决办法
5326
查看次数

DependencyProperty.OverrideMetadata 覆盖了 PropertyChangedCallback

DependencyProperty.OverrideMetadata() 是否也覆盖 PropertyChangedCallback ?

silverlight wpf dependency-properties

2
推荐指数
1
解决办法
1009
查看次数

WPF简单TextBox绑定 - 从未触及依赖属性

我有一个TextBox,我试图将它绑定到一个DependencyProperty.该物业永远不会在装载或我输入时触及TextBox.我错过了什么?

XAML

<UserControl:Class="TestBinding.UsernameBox"
        // removed xmlns stuff here for clarity>
    <Grid>
        <TextBox Height="23" Name="usernameTextBox" Text="{Binding Path=Username, ElementName=myWindow, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

C#

public partial class UsernameBox : UserControl
{
    public UsernameBox()
    {
        InitializeComponent();
    }

    public string Username
    {
        get
        {
            // Control never reaches here
            return (string)GetValue(UsernameProperty);
        }
        set
        {
            // Control never reaches here
            SetValue(UsernameProperty, value);
        }
    }

    public static readonly DependencyProperty UsernameProperty
        = DependencyProperty.Register("Username", typeof(string), typeof(MainWindow));
}
Run Code Online (Sandbox Code Playgroud)

编辑:我需要实现一个DependencyProperty因为我正在创建自己的控件.

wpf binding textbox dependency-properties

2
推荐指数
1
解决办法
9512
查看次数

依赖属性继承

我需要我的控件从Grid类型的祖先那里继承UIElement.IsEnabledProperty(可以选择Window或其他任何我可以用来包装我的网格的元素)

CS:在下面,我重写UIElement.IsEnabledProperty的元数据,并使用Change和Coerce委托对其进行设置。

    static PipeControl()
    {
        PipeControl.IsEnabledProperty.OverrideMetadata(typeof(PipeControl), new FrameworkPropertyMetadata(false, OnIsEnabledPropertyChanged, OnIsEnabledPropertyCoerce));
    }

    private static void OnIsEnabledPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var isEnabled = (bool)e.NewValue;
    }

    private static object OnIsEnabledPropertyCoerce(DependencyObject d, object baseValue)
    {
        var valueSource = DependencyPropertyHelper.GetValueSource(d, PipeControl.IsEnabledProperty);

        var pipeContorl = d as PipeControl;
        if (pipeContorl == null) return baseValue;

        return (bool)baseValue && pipeContorl.IsMyPipe;
    }
Run Code Online (Sandbox Code Playgroud)

XAML:

    <Grid IsEnabled="{Binding IsMyCondition , Mode=OneWay}">        
         <game:PipeControl Grid.Row="2"  />            
         <game:PipeControl  Grid.Row="2" Grid.Column="1" />
    </Grid> 
Run Code Online (Sandbox Code Playgroud)

每次IsMyCondition更改时,每个PipeContorl中都会调用一次OnIsEnabledPropertyCoerce,从不调用OnIsEnabledPropertyChanged,OnIsEnabledProerty Coerce中的ValueSource为“ Default”(显示Coerce始终获取默认的假值)。

我必须以需要使用继承的方式错过某些东西,我希望值源被“继承”,并调用OnIsEnabledPropertyChanged。

wpf inheritance dependency-properties custom-controls

2
推荐指数
1
解决办法
3508
查看次数

获取UIElement或发件人的背景

我正在尝试找到一种在UIElement上设置Background属性的通用方法.

我运气不好......

这是我到目前为止(尝试使用反射来获取BackgroundProperty).

  Action<UIElement> setTheBrushMethod = (UIElement x) =>
  {
    var brush = new SolidColorBrush(Colors.Yellow);
    var whatever = x.GetType().GetField("BackgroundProperty");
    var val = whatever.GetValue(null);
    ((UIElement)x).SetValue(val as DependencyProperty, brush);
    brush.BeginAnimation(SolidColorBrush.ColorProperty, new ColorAnimation(Colors.White, TimeSpan.FromSeconds(3)));
  };
  setTheBrushMethod(sender as UIElement);
Run Code Online (Sandbox Code Playgroud)

事情是......它适用于类似TextBlock的东西,但不适用于像StackPanel或Button这样的东西.

对于StackPanel或Button,"whatever"最终为null.

我也觉得应该有一个简单的方法来一般设置背景.我错过了吗?

后台似乎只在System.Windows.Controls.Control上可用,但我无法转换为.

c# wpf background dependency-properties uielement

2
推荐指数
1
解决办法
1919
查看次数

属性在DependencyProperty中更改

在上一篇文章中,我询问了如何将属性注册为DependencyProperty.我得到了答案,它工作正常.

但是现在我想在Click上为这个DependencyProperty添加一些Items.这不起作用.我注册DependencyProperty的代码是:

public static readonly DependencyProperty ChartEntriesProperty = DependencyProperty.Register(
        "ChartEntries", typeof(ObservableCollection<ChartEntry>), typeof(ChartView),
        new FrameworkPropertyMetadata(OnChartEntriesChanged));

    private static void OnChartEntriesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {

    }
Run Code Online (Sandbox Code Playgroud)

在从我的XAML绑定到我的c#-code时,调用OnChartEntriesChanged-Event.但是如果我之后添加ChartEntry(按下按钮),则不会触发事件.

有谁知道为什么?

c# wpf dependency-properties

2
推荐指数
1
解决办法
1117
查看次数