问题
I'm having a WPF Custom Control
<local:SuperControl>
<local:SuperControl.SBItem>
<MultiBinding StringFormat="{}Name: {0} ({1})">
<Binding Path="Name" />
<Binding Path="ID" />
</MultiBinding>
</local:SuperControl.SBItem>
</local:SuperControl>
The ViewModel Property
public string Name { get; set; }
public string ID { get; set; }
Consider the Value for the Property
Name = "John";
ID = "STK001";
The Custom Control
public class SuperControl : ItemsControl
{
public static readonly DependencyProperty SBItemProperty = DependencyProperty.Register("SBItem", typeof(string), typeof(BAutoComplete), new PropertyMetadata(null));
public string SBItem
{
get { return (string)GetValue(SBItemProperty); }
set { SetValue(SBItemProperty, value); }
}
public override void OnApplyTemplate()
{
string Name = SBItem;
string ID = SBItem;
string StringFormat = SBItem;
}
}
Consider the Piece of Code in the Custom Control
public override void OnApplyTemplate()
{
string Name = SBItem;
string ID = SBItem;
string StringFormat = SBItem;
}
Here I need to get the Value of the Binded Property Name
, ID
and String Format
from the Dependency Property SBItem
. Kindly assist me.
回答1:
You cannot get Binded values in ApplyTemplate
method. As it is called before binding.
So, provide a callback for property change using new PropertyMetadata(null,new PropertyChangedCallback(OnPropertyChanged))
in your DP definition.
private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
string value = (string)e.NewValue;
string Name = value.Split(new char[] { ':' })[1].Split(new char[] { '(' })[0].Trim();
string ID = value.Split(new char[] { ':' })[1].Split(new char[] { '(' })[1].Split(new char[] { ')' })[0].Trim();
string formatting = BindingOperations.GetMultiBinding(d, MyButton.MyPropertyProperty).StringFormat;
}
回答2:
As AnjumSKhan already stated, you have to implement a Changed
-Method for the DependencyProperty
. Following you'll find a more generic approach on how you can get the bound Values.
public static readonly DependencyProperty SBItemProperty = DependencyProperty.Register("SBItem", typeof(string), typeof(BAutoComplete), new PropertyMetadata(Changed));
private static void Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) {
MultiBindingExpression mbe = null;
try {
mbe = BindingOperations.GetMultiBindingExpression(d, e.Property);
} catch { }
if (mbe != null) {
foreach (var beb in mbe.BindingExpressions) {
var bindingExpressionBase = (BindingExpression)beb;
var di = bindingExpressionBase.DataItem;
var p = di.GetType().GetProperty(bindingExpressionBase.ResolvedSourcePropertyName);
var val = p.GetValue(di);
Console.WriteLine($"Property: {p.Name} Value: {val}");
}
} else {
try {
var binding = BindingOperations.GetBindingExpression(d, e.Property);
var di = binding.DataItem;
var p = di.GetType().GetProperty(binding.ResolvedSourcePropertyName);
var val = p.GetValue(di);
Console.WriteLine($"Property: {e.Property.Name} Value: {val}");
} catch {
Console.WriteLine("No binding found");
}
}
}
Note
Of course you have to replace my Console.WriteLine
with your own logic. This example shall demonstrate, how you can get the values from the bound source. Probably you can ignore the part i've implemented as fallback if the Binding is a normal one and not a Multibinding.
Hope this gets you in the right direction
来源:https://stackoverflow.com/questions/39462765/find-list-of-binded-property-in-a-depedency-property-in-wpf-c-sharp