如何提高依赖财产上更改财产的事件?

问题描述 投票:0回答:5

I具有两个属性的控制。一个是一个,另一个是第一个的“别名”。 当第一个更改第一个事件时,我如何提高第二个事件(别名)。

注:我正在使用
DependencyProperty
,而不是

PropertyChanged

(尝试过,因为我的控件是一个子类) 像这样的东西.....

DependencyObjects
如果我正在使用一个inotify,我可以这样做...
INotifyPropertyChanged

在哪里可以从一个设置器上提出有关多个属性的事件? 我需要能够做同样的事情,只使用

ListVie

	

我遇到了一个类似的问题,我有一个依赖关系属性,我希望该类聆听更改事件以从服务中获取相关数据。

protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e) { base.OnPropertyChanged(e); if (e.Property == MyFirstProperty) { RaiseAnEvent( MySecondProperty ); /// what is the code that would go here? } }
    

thimplement
public string SecondProperty { get { return this.m_IconPath; } } public string IconPath { get { return this.m_IconPath; } set { if (this.m_IconPath != value) { this.m_IconPath = value; this.SendPropertyChanged("IconPath"); this.SendPropertyChanged("SecondProperty"); } } }
在您的班级中。

注册依赖项属性时,请在属性元数据中指定回调。
wpf binding
5个回答
84
投票
在回调中,提出

PropertyChanged

活动。
添加回调:

49
投票
DependencyProperties
  1. 在回调中进行priansing

    public static readonly DependencyProperty CustomerProperty = DependencyProperty.Register("Customer", typeof(Customer), typeof(CustomerDetailView), new PropertyMetadata(OnCustomerChangedCallBack)); public Customer Customer { get { return (Customer)GetValue(CustomerProperty); } set { SetValue(CustomerProperty, value); } } private static void OnCustomerChangedCallBack( DependencyObject sender, DependencyPropertyChangedEventArgs e) { CustomerDetailView c = sender as CustomerDetailView; if (c != null) { c.OnCustomerChanged(); } } protected virtual void OnCustomerChanged() { // Grab related data. // Raises INotifyPropertyChanged.PropertyChanged OnPropertyChanged("Customer"); }

    INotifyPropertyChanged
  2. 我认为OP提出了错误的问题。下面的代码将表明,无需从依赖项属性中手动提出
  3. PropertyChanged
  4. 事件以实现所需的结果。这样做的方法是处理依赖关系属性的回调,并为那里的其他依赖关系属性设置值。以下是一个工作示例。 在下面的代码中,

    public static DependencyProperty FirstProperty = DependencyProperty.Register( "First", typeof(string), typeof(MyType), new FrameworkPropertyMetadata( false, new PropertyChangedCallback(OnFirstPropertyChanged)));

    具有两个依赖关系属性-Y
    PropertyChanged

  5. private static void OnFirstPropertyChanged( DependencyObject sender, DependencyPropertyChangedEventArgs e) { PropertyChangedEventHandler h = PropertyChanged; if (h != null) { h(sender, new PropertyChangedEventArgs("Second")); } }
。当用户单击主机上的按钮(

PropertyChanged

)上时,修改了
PropertyChanged
。依赖项属性上的

MyControl

回调设置了
ActiveTabInt

的值。
ActiveTabString
事件不是由

11
投票
.

手动提出的。

mainWindow.xaml.cs
ActiveTabString
mainWindow.xaml
PropertyChanged
app.xaml
ActiveTabInt

我同意山姆和Xaser的观点,实际上对此有所更远。我认为您根本不应该在
PropertyChanged
中实现
MyControl
接口...控件已经是一个
/// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); DataContext = this; ActiveTabString = "zero"; } private string _ActiveTabString; public string ActiveTabString { get { return _ActiveTabString; } set { if (_ActiveTabString != value) { _ActiveTabString = value; RaisePropertyChanged("ActiveTabString"); } } } private int _ActiveTabInt; public int ActiveTabInt { get { return _ActiveTabInt; } set { if (_ActiveTabInt != value) { _ActiveTabInt = value; RaisePropertyChanged("ActiveTabInt"); } } } #region INotifyPropertyChanged implementation public event PropertyChangedEventHandler PropertyChanged; public void RaisePropertyChanged(string propertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } #endregion private void Button_Click(object sender, RoutedEventArgs e) { ActiveTabString = (ActiveTabString == "zero") ? "one" : "zero"; } } public class MyControl : Control { public static List<string> Indexmap = new List<string>(new string[] { "zero", "one" }); public string ActiveTabString { get { return (string)GetValue(ActiveTabStringProperty); } set { SetValue(ActiveTabStringProperty, value); } } public static readonly DependencyProperty ActiveTabStringProperty = DependencyProperty.Register( "ActiveTabString", typeof(string), typeof(MyControl), new FrameworkPropertyMetadata( null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, ActiveTabStringChanged)); public int ActiveTabInt { get { return (int)GetValue(ActiveTabIntProperty); } set { SetValue(ActiveTabIntProperty, value); } } public static readonly DependencyProperty ActiveTabIntProperty = DependencyProperty.Register( "ActiveTabInt", typeof(Int32), typeof(MyControl), new FrameworkPropertyMetadata( new Int32(), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); static MyControl() { DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl))); } public override void OnApplyTemplate() { base.OnApplyTemplate(); } private static void ActiveTabStringChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { MyControl thiscontrol = sender as MyControl; if (Indexmap[thiscontrol.ActiveTabInt] != thiscontrol.ActiveTabString) thiscontrol.ActiveTabInt = Indexmap.IndexOf(e.NewValue.ToString()); } }
,因此已经带有通知。将
<StackPanel Orientation="Vertical"> <Button Content="Change Tab Index" Click="Button_Click" Width="110" Height="30"></Button> <local:MyControl x:Name="myControl" ActiveTabInt="{Binding ActiveTabInt, Mode=TwoWay}" ActiveTabString="{Binding ActiveTabString}"></local:MyControl> </StackPanel>

添加到

<Style TargetType="local:MyControl"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="local:MyControl"> <TabControl SelectedIndex="{Binding ActiveTabInt, Mode=TwoWay}"> <TabItem Header="Tab Zero"> <TextBlock Text="{Binding ActiveTabInt}"></TextBlock> </TabItem> <TabItem Header="Tab One"> <TextBlock Text="{Binding ActiveTabInt}"></TextBlock> </TabItem> </TabControl> </ControlTemplate> </Setter.Value> </Setter> </Style>

是多余的,对我来说是错误的。 正如Sam所建议的那样,我所做的就是实现这两个属性,但随后简单地从“第一个”依赖项属性中使用了“第二”依赖关系属性的值。由于这两者都是依赖性属性,因此两者都会自动向任何感兴趣的订户(例如数据绑定等)提出更改通知。
在这种情况下,依赖关系属性a是字符串,它触发了依赖关系b的更改,即名为
INotifyPropertyChanged

。如果您有一些文本,您希望能够通过数据绑定完全隐藏在控件中。

UserControl
注意,我在这里不包括

DependencyObject

签名或构造函数,因为它们没有什么特别之处。他们根本不需要从
INotifyPropertyChanged

6
投票

建造以前接受的答案,对我而言,演员们缺少了访问非静态的

DependencyObject
thimplement
DependencyProperties
在您的班上,例如
PropertyChangedCallback

注册时在属性元数据中指定回调
依赖性属性
在回调中,投掷和提高

InviteText

活动。


添加回调:
Visibility

播放发件人并在回调中筹集了变化的属性:
ShowInvite


i我质疑在第二个属性变化时,在第二个属性上提出
public string InviteText { get { return (string)GetValue(InviteTextProperty); } set { SetValue(InviteTextProperty, value); } } public static readonly DependencyProperty InviteTextProperty = DependencyProperty.Register("InviteText", typeof(string), typeof(InvitePrompt), new UIPropertyMetadata(String.Empty, OnInviteTextChanged)); private static void OnInviteTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { InvitePrompt prompt = d as InvitePrompt; if (prompt != null) { string text = e.NewValue as String; prompt.ShowInvite = String.IsNullOrWhiteSpace(text) ? Visibility.Collapsed : Visibility.Visible; } } public Visibility ShowInvite { get { return (Visibility)GetValue(ShowInviteProperty); } set { SetValue(ShowInviteProperty, value); } } public static readonly DependencyProperty ShowInviteProperty = DependencyProperty.Register("ShowInvite", typeof(Visibility), typeof(InvitePrompt), new PropertyMetadata(Visibility.Collapsed));

事件的逻辑。 如果第二个属性值更改,则可以在此处提出

1
投票
事件。

任何比率,您的问题的答案是您应该实施

INotifyPropertyChanged
。此界面包含

PropertyChanged
    事件。实施
  1. INotifyPropertyChanged

    让其他代码知道类具有

    UserControl CustomView
    事件,因此代码可以连接处理程序。实现
    PropertyChanged
    后,您的if语句中的代码是:

  2. public static DependencyProperty FirstProperty = DependencyProperty.Register( "First", typeof(string), typeof(MyType), new FrameworkPropertyMetadata( false, new PropertyChangedCallback(OnFirstPropertyChanged)));
  3. 这听起来很疯狂。 再说一次,我不是电脑极客。 这可以帮助掩盖欺诈的东西吗? 也许是一个愚蠢的问题,但这一切对我来说有点腥。当您提到财产时,您可以谈论不动产或兴趣吗? 抱歉,如果我好奇的话。
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.