标签: dependency-properties

Dependency property not updating usercontrol ui

I have a usercontrol which has a couple of textblocks on it

<UserControl x:Class="Tester.Messenger"
         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" 
         mc:Ignorable="d" 
         x:Name="myUserControl"
         >  
<TextBlock Text="{Binding ElementName=myUserControl,Path=Header,Mode=TwoWay}" Foreground="LightGray" FontSize="11"  Margin="3,3,0,-3"/>
<TextBlock Grid.Row="1" Grid.ColumnSpan="2" Text="{Binding ElementName=myUserControl,Path=Message, Mode=TwoWay}" Foreground="White" FontSize="16" Margin="3,-5"/>
Run Code Online (Sandbox Code Playgroud)

In my code behind I have two dependency properties that I'm binding the above textblocks Text property to.

public static readonly DependencyProperty HeaderProperty =
        DependencyProperty.Register("HeaderProperty", typeof(string), typeof(UserControl), new PropertyMetadata("header"));

    public static readonly DependencyProperty MessageProperty =
        DependencyProperty.Register("MessageProperty", typeof(string), typeof(UserControl), new PropertyMetadata(null));


 public string Header
    { …
Run Code Online (Sandbox Code Playgroud)

silverlight wpf user-controls dependency-properties

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

当依赖属性具有 RelativeSource 绑定时,GetTemplateChild 返回 null

我创建了一个自定义控件,MyTextBoxTextBox. 它有一个与之关联的样式,其中包含一个命名控件:

<Style x:Key="{x:Type MyTextBox}" TargetType="{x:Type MyTextBox}">
    <!-- ... -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type MyTextBox}">
                <!-- ... -->
                    <SomeControl x:Name="PART_SomeControl" />
                <!-- ... -->
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)

MyTextBox有一个依赖属性,当设置时,将其值传播到SomeControl

public class MyTextBox : TextBox
{
    // ...

    public static new readonly DependencyProperty MyParameterProperty =
        DependencyProperty.Register(
            "MyParameter",
            typeof(object),
            typeof(MyTextBox),
            new PropertyMetadata(default(object), MyParameterChanged));

    private static void MyParameterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var me = (MyTextBox)d;
        var someControl = (SomeControl)me.GetTemplateChild("PART_SomeControl");
        someControl.SetValue(SomeControl.MyParameterProperty, e.NewValue);
    }
}
Run Code Online (Sandbox Code Playgroud)

这在进行简单绑定时工作正常,如下所示: …

c# wpf xaml dependency-properties relativesource

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

为什么我的依赖属性绑定没有按预期工作?

我正在尝试制作一个包含 ListView 的自定义 UserControl。我想将 SelectedItem 属性绑定到我的视图模型。因此,我在用户控件中创建了一个 DP,并将 ListView 的 SelectedItem 属性绑定到新的依赖属性。

        public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.Register(
            "SelectedItem",
            typeof(object), 
            typeof(DragAndDropListView),
            new PropertyMetadata(SelectedItemPropertyChangedCallback));

        private static void SelectedItemPropertyChangedCallback(DependencyObject d, 
        DependencyPropertyChangedEventArgs e)
        {
            if (d is DragAndDropListView dragAndDropListView)
            {
            }
        }

        public object SelectedItem
        {
            get => GetValue(SelectedItemProperty);
            set => SetValue(SelectedItemProperty, value);
        }
Run Code Online (Sandbox Code Playgroud)

然后在 xaml 代码中我进行了绑定

            <ListView Name="ListView"
                      PreviewMouseLeftButtonDown="ListViewPreviewMouseLeftButtonDown"
                      AllowDrop="True"
                      MouseMove="ListViewMouseMove"
                      DragEnter="ListViewDragEnter"
                      Drop="ListViewDrop"
                      SelectedItem="{Binding Path= SelectedItem}"/>
Run Code Online (Sandbox Code Playgroud)

为了确保它有效,我将 UserControl 作为 DataContext 分配给 ListView

        public DragAndDropListView()
        {
            InitializeComponent();
            ListView.ItemsSource = Items;
            ListView.DataContext = this; …
Run Code Online (Sandbox Code Playgroud)

c# wpf binding dependency-properties mvvm

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

WPF:关于依赖属性的基本问题

我在Window(ArtistInfo)中有以下Xaml :

<Grid>
    <TextBlock Text="{Binding Artist.Name}"></TextBlock>
</Grid>
Run Code Online (Sandbox Code Playgroud)

这是同一个窗口的代码隐藏(为了问题简化了代码):

public static readonly DependencyProperty ArtistProperty = 
        DependencyProperty.Register("Artist", typeof(Artist), typeof(ArtistInfo));

Artist Artist {
    get {
        return (Artist)GetValue(ArtistProperty);
    }
    set {
        SetValue(ArtistProperty, value);
    }
}

public ArtistInfo() {
    InitializeComponent();
}
public ArtistInfo(int artistID) {
    InitializeComponent();
    Artist = GetArtist(artistID);
}
Run Code Online (Sandbox Code Playgroud)

基本上我正在尝试做的是数据绑定到依赖属性,因此当Artist填充(在构造函数中)时,TextBlock将填充Artist的名称.

我在这里错过了什么?

c# data-binding wpf xaml dependency-properties

0
推荐指数
1
解决办法
463
查看次数

没有DependencyProperties与实际属性冲突的最佳方法是什么?

我发现当我创建依赖属性时,大多数都与UserControl中的属性名称冲突,例如背景,宽度等.所以我的策略是在我的所有自定义属性前加上"The",所以我有,例如

  • 的背景
  • 宽度

等等

我尝试使用"new"关键字来消除警告,但这会导致运行时发生冲突.

有没有人在自定义用户控件中为DependencyProperties提供更好的命名策略?

public partial class SmartForm : UserControl
{
    public SmartForm()
    {
        InitializeComponent();
        DataContext = this;

        TheBackground = "#FFD700";
    }

    #region DependencyProperty: TheBackground
    public string TheBackground
    {
        get
        {
            return (string)GetValue(TheBackgroundProperty);
        }
        set
        {
            SetValue(TheBackgroundProperty, value);
        }
    }

    public static readonly DependencyProperty TheBackgroundProperty =
        DependencyProperty.Register("TheBackground", typeof(string), typeof(SmartForm),
        new FrameworkPropertyMetadata());
    #endregion
}
Run Code Online (Sandbox Code Playgroud)

wpf user-controls dependency-properties

0
推荐指数
1
解决办法
86
查看次数

如何在不在UI线程上获取silverlight依赖属性?

我在这里讨论的问题基本相同:http://khason.net/blog/dependency-property-getters-and-setters-in-multithreaded-environment/

public static readonly DependencyProperty MyPropertyProperty =
        DependencyProperty.RegisterAttached("MyProperty", typeof(bool),
        typeof(MyObject), new PropertyMetadata(new PropertyChangedCallback(OnMyPropertyChanged)));

public static bool GetMyProperty(DependencyObject obj)
{
    return (bool)obj.GetValue(MyPropertyProperty);     <<<<<
}

public static void SetMyProperty(DependencyObject obj, bool value)
{
    obj.SetValue(MyPropertyProperty, value);
}
Run Code Online (Sandbox Code Playgroud)

如果标记为"<<<<<"的行从后台线程调用,Silverlight会抛出InvalidOperationException,我的应用程序可能会死锁.

遗憾的是,博客文章中的解决方案无效,因为Dispatcher类的Silverlight版本隐藏了同步的Invoke方法 - 只有BeginInvoke被标记为public.

silverlight multithreading asynchronous dependency-properties

0
推荐指数
1
解决办法
631
查看次数

使用INotifyPropertyChanged而不是DependencyProperties进行数据绑定

我一直在努力让数据绑定在WPF中工作一周多一点.我在这里得到了关于DataContext的宝贵帮助,我确实通过DependencyProperties获得了数据绑定.在我学习数据绑定的同时,我INotifyPropertyChanged在很多方面遇到了很多关于它以及如何比DP更好的讨论.我想我会试一试并尝试一下.

我正在使用Josh Smith的基础ViewModel类,我的ViewModel是从它派生的.但是,我在使数据绑定工作时遇到了一些麻烦,我希望有人可以告诉我哪里出错了.

在我的ViewModel类中,我有一个ObservableCollection<string>.在我的GUI中,我有一个与此OC绑定的组合框,即

<ComboBox ItemsSource="{Binding PluginNames}" />
Run Code Online (Sandbox Code Playgroud)

GUI的DataContext设置为ViewModel,即

private ViewModel _vm;

public GUI()
{
  InitializeComponent();
  _vm = new ViewModel();
  this.DataContext = _vm;
}
Run Code Online (Sandbox Code Playgroud)

并且ViewModel的OC名为"PluginNames":

public class ViewModel
{
  public ObservableCollection<string> PluginNames;  // this gets instantiated and added to elsewhere
}
Run Code Online (Sandbox Code Playgroud)

加载GUI时,会调用一个实例化OC并将插件名称添加到其中的方法.OC被修改后,我打电话给RaisePropertyChanged( "PluginNames").我期待由于WPF数据绑定模型认识到INotifyPropertyChanged,这就是我需要做的所有事情,它将"神奇地工作"并使用已加载的插件更新组合框项目......但事实并非如此.

有谁可以指出我在这里做错了什么?谢谢!

更新:我不知道为什么,但现在没有做任何明显的更新,它根本没有找到属性.我想我真的很傻,错过了某个重要的一步.

wpf dependency-properties mvvm inotifypropertychanged

0
推荐指数
1
解决办法
570
查看次数

动画属性并查看其后面的代码更改

我经常需要为wpf中不属于视图的一些内容设置动画,例如计算机的音量或鼠标的位置.我想通过使用wpf故事板和内置的缓动功能来实现.

例如,假设我想使用以下故事板在我的计算机上设置动画(减小音量):

<Storyboard x:Key="Storyboard1">
        <DoubleAnimationUsingKeyFrames 
                     Storyboard.TargetProperty="someProperty"  
                     Storyboard.TargetName="SomeTarget">
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="0">
                <EasingDoubleKeyFrame.EasingFunction>
                    <CircleEase EasingMode="EaseOut"/>
                </EasingDoubleKeyFrame.EasingFunction>
            </EasingDoubleKeyFrame>
        </DoubleAnimationUsingKeyFrames>
</Storyboard>
Run Code Online (Sandbox Code Playgroud)

在我的代码后面我用函数设置音量:

  MyVolumeController.SetVolume(0);
Run Code Online (Sandbox Code Playgroud)

因此,我想创建一个看起来像这样的函数:(注意函数是某种伪代码)

 public void AnimateProperty(Storyboard sb, Action<double> onPropertyChange)
 {
      var property = //sb.targetProperty;

      property.OnValueChanged += (a,b)=>{

           onPropertyChange(b.newValue);

      }

      sb.begin();// start animating

 }
Run Code Online (Sandbox Code Playgroud)

然后我可以通过调用该方法为该动画设置动画:

  AnimateProperty(
           (Storyboard)this.FindResource("Storyboard1"), // storyboard
           (newVal)=>{MyVolumeController.SetVolume(newVal) // action
  );
Run Code Online (Sandbox Code Playgroud)

c# wpf dependency-properties storyboard

0
推荐指数
1
解决办法
315
查看次数

生产者参数注入?

如何创建一个根据参数创建对象的生成器方法?

我的目标是能够CrudService在我的应用程序中注入不同的类,但是使用服务所用的类(例如User.class)来对其进行参数化.

以下代码当然不起作用,但说明了我的意图.

@Produces
@JPAContainer(Class type) //something like this?
public JPAContainer getJPA() {
    @PersistenceContext
    private EntityManager em;

    @Produces
    @JPAContainerAnnot
    public JPAContainer getJPAContainer() {
       return JPAContainerFactory.make(type, em); //eg: class = User.class, Person.class
    }
}

@Stateless
public class CrudServiceUser() {
   @Inject
   @JPAContainer(type = User.class) //something like this parameter
   private JPAContainer container;
}

@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD,METHOD,PARAMETER,TYPE})
public @interface JPAContainer {

}
Run Code Online (Sandbox Code Playgroud)

java jpa dependency-properties java-ee cdi

0
推荐指数
1
解决办法
543
查看次数

使用DependencyProperty在TextBox上添加IsDirty-Flag

我有问题,我有一个我无法扩展的现有模型对象.实际问题有点复杂,所以我试图将其分解.

我想扩展一个TextBox依赖属性来指示文本已更改.所以我想出了以下解决方案:

public class MyTextField : TextBox
{
    public MyTextField()
    {
        this.TextChanged += new TextChangedEventHandler(MyTextField_TextChanged);
    }

    private void MyTextField_TextChanged(object sender, TextChangedEventArgs e)
    {
        IsDirty = true;
    }
    public static DependencyProperty IsDirtyProperty = DependencyProperty.Register(
        "IsDirtyProperty",
        typeof(bool),
        typeof(MyTextField),
        new PropertyMetadata(false));

    public bool IsDirty
    {
        get { return (bool)GetValue(IsDirtyProperty); }
        set { SetValue(IsDirtyProperty, value); }
    }
}
Run Code Online (Sandbox Code Playgroud)

XAML:

<my:MiaTextField Text="{Binding Barcode}" IsDirty="{Binding IsDirty}"/>
Run Code Online (Sandbox Code Playgroud)

因此,如果我更改了文本TextBox,则isDirty属性应更改为true.但我得到了一个System.Windows.Markup.XamlParseException:绑定只能设置"DependencyObject"的"DependencyProperty".

c# wpf dependency-properties

0
推荐指数
1
解决办法
118
查看次数