Rob*_*Rob 5 c# data-binding wpf xaml
我有一个字符串依赖属性(SearchText),更新后,需要更新集合依赖属性(结果).
我的收藏dp:
public IEnumerable<string> Results{
get { return (IEnumerable<string>) GetValue(ResultsProperty); }
set { SetValue(ResultsProperty, value); }
}
public static readonly DependencyProperty ResultsProperty=
DependencyProperty.Register("Results", typeof(IEnumerable<string>), typeof(MainWindowVM), new UIPropertyMetadata(new List<string>()));
Run Code Online (Sandbox Code Playgroud)
我试了这个没有运气.我在结果= ....线上放了一个断点,它从未被击中.
public string SearchText{
get { return (string) GetValue(SearchTextProperty); }
set {
Results =
from T in Tree.GetPeople(value)
select T.FullName;
SetValue(SearchTextProperty, value);
}
}
public static readonly DependencyProperty SearchTextProperty=
DependencyProperty.Register("SearchText", typeof(string), typeof(MainWindowVM), new UIPropertyMetadata(""));
Run Code Online (Sandbox Code Playgroud)
XAML:
<TextBox DockPanel.Dock="Top" Text="{Binding SearchValue, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<ListBox DockPanel.Dock="Top" ItemsSource="{Binding NameResults}" SelectedItem="{Binding Search}" />
在XAML中设置依赖项属性或通过绑定时,运行时将始终绕过实例属性别名并直接调用GetValue和SetValue.正因为如此,您的实例设置器未被调用.
您可能希望考虑的是使用依赖项属性注册方法,该属性将在属性更改时调用.在PropertyMetadata为依赖项属性创建时,这是最容易完成的.
我相信以下示例可以满足您的需求.在该示例中,我的类具有两个依赖属性别名为First和Second.当我为First设置值时,我的变更处理程序被调用,我将值设置为Second.
public class DependencyPropertyTest : DependencyObject
{
public static readonly DependencyProperty FirstProperty;
public static readonly DependencyProperty SecondProperty;
static DependencyPropertyTest()
{
FirstProperty = DependencyProperty.Register("FirstProperty",
typeof(bool),
typeof(DependencyPropertyTest),
new PropertyMetadata(false, FirstPropertyChanged));
SecondProperty = DependencyProperty.Register("SecondProperty",
typeof(string),
typeof(DependencyPropertyTest),
new PropertyMetadata(null));
} // End constructor
private bool First
{
get { return (bool)this.GetValue(FirstProperty); }
set { this.SetValue(FirstProperty, value); }
} // End property First
private string Second
{
get { return (string)this.GetValue(SecondProperty); }
set { this.SetValue(SecondProperty, value); }
} // End property Second
private static void FirstPropertyChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs ea)
{
DependencyPropertyTest instance = dependencyObject as DependencyPropertyTest;
if (instance == null)
{
return;
}
instance.Second = String.Format("First is {0}.", ((bool)ea.NewValue).ToString());
} // End method FirstPropertyChanged
} // End class DependencyPropertyTest
Run Code Online (Sandbox Code Playgroud)
我希望有所帮助.
正如我评论的那样,当前接受的答案在原则上是正确的,除了它必须使用该SetCurrentValue方法而不是做一个简单的赋值.有关它的更多解释,请参阅此答案.
以下是与此修复相同的代码:
public class DependencyPropertyTest : DependencyObject
{
public static readonly DependencyProperty FirstProperty;
public static readonly DependencyProperty SecondProperty;
static DependencyPropertyTest()
{
FirstProperty = DependencyProperty.Register("FirstProperty",
typeof(bool),
typeof(DependencyPropertyTest),
new PropertyMetadata(false, FirstPropertyChanged));
SecondProperty = DependencyProperty.Register("SecondProperty",
typeof(string),
typeof(DependencyPropertyTest),
new PropertyMetadata(null));
} // End constructor
private bool First
{
get { return (bool)this.GetValue(FirstProperty); }
set { this.SetValue(FirstProperty, value); }
} // End property First
private string Second
{
get { return (string)this.GetValue(SecondProperty); }
set { this.SetValue(SecondProperty, value); }
} // End property Second
private static void FirstPropertyChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs ea)
{
DependencyPropertyTest instance = dependencyObject as DependencyPropertyTest;
if (instance == null)
{
return;
}
// SetCurrentValue should be used here!
instance.SetCurrentValue(SecondProperty,
String.Format("First is {0}.", ((bool)ea.NewValue).ToString());
} // End method FirstPropertyChanged
} // End class DependencyPropertyTest
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5316 次 |
| 最近记录: |