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");
}
}
}
在您的班级中。注册依赖项属性时,请在属性元数据中指定回调。
PropertyChanged
添加回调:
DependencyProperties
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
我认为OP提出了错误的问题。下面的代码将表明,无需从依赖项属性中手动提出
PropertyChanged
public static DependencyProperty FirstProperty = DependencyProperty.Register(
"First",
typeof(string),
typeof(MyType),
new FrameworkPropertyMetadata(
false,
new PropertyChangedCallback(OnFirstPropertyChanged)));
具有两个依赖关系属性-Y
PropertyChanged
和private static void OnFirstPropertyChanged(
DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
PropertyChangedEventHandler h = PropertyChanged;
if (h != null)
{
h(sender, new PropertyChangedEventArgs("Second"));
}
}
PropertyChanged
PropertyChanged
。依赖项属性上的MyControl
回调设置了
ActiveTabInt
的值。 ActiveTabString
事件不是由手动提出的。
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>
在这种情况下,依赖关系属性a是字符串,它触发了依赖关系b的更改,即名为
INotifyPropertyChanged
。如果您有一些文本,您希望能够通过数据绑定完全隐藏在控件中。
UserControl
注意,我在这里不包括
DependencyObject
INotifyPropertyChanged
建造以前接受的答案,对我而言,演员们缺少了访问非静态的
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));
事件的逻辑。 如果第二个属性值更改,则可以在此处提出任何比率,您的问题的答案是您应该实施
INotifyPropertyChanged
。此界面包含PropertyChanged
INotifyPropertyChanged
让其他代码知道类具有
UserControl CustomView
事件,因此代码可以连接处理程序。实现PropertyChanged
后,您的if语句中的代码是:
public static DependencyProperty FirstProperty = DependencyProperty.Register(
"First",
typeof(string),
typeof(MyType),
new FrameworkPropertyMetadata(
false,
new PropertyChangedCallback(OnFirstPropertyChanged)));