我试图运行下面附加的文章“Dependency Properties in WPF”中的代码。在线SetValue(MyDependencyProperty, value);
如何更正此示例说明WPF DependencyProperty用法?
但应用程式的空档,出现异常:
System.Windows.Markup.XamlParseException
"'' is not a valid value for property 'MyProperty'."
内部异常:
{“_3DP_CallBack_DefaultValue.MainWindow '符合指定 '构造对 类型调用'绑定约束抛出了一个异常。'行号 '3' 和线 位置 '9'。“}
我应该怎么才能运行这个程序改变?
WPF应用程序的代码:
namespace _3DP_CallBack_DefaultValue
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DependencyPropertySample dpSample = new DependencyPropertySample();
dpSample.MyProperty = "Dependency Property Test";//???
Binding mybinding = new Binding("MyProperty");
mybinding.Mode = BindingMode.OneWay;
mybinding.Source = dpSample;
BindingOperations.SetBinding(MyTextblock, TextBox.TextProperty, mybinding);
}
}
public class DependencyPropertySample : DependencyObject
{
//Register Dependency Property
public static readonly DependencyProperty MyDependencyProperty
= DependencyProperty.Register
(
"MyProperty", typeof(string), typeof(DependencyPropertySample),
new PropertyMetadata
(
"Test",
new PropertyChangedCallback(OnMyPropertyChanged),
new CoerceValueCallback(OnCoerceValue)
),
new ValidateValueCallback(OnValidateMyProperty)
);
public string MyProperty
{
get
{
return (string)GetValue(MyDependencyProperty);
}
set
{
//***************************
//breaking on the following line trying to set any string value
// in this case "Dependency Property Test"
SetValue(MyDependencyProperty, value);
}
}
public static void OnMyPropertyChanged(DependencyObject dObject,
DependencyPropertyChangedEventArgs e)
{
MessageBox.Show(e.NewValue.ToString());
}
public static string OnCoerceValue(DependencyObject dObject, object val)
{
if (val.ToString().CompareTo("Test") == 1)
{
return val.ToString();
}
return string.Empty;
}
public static bool OnValidateMyProperty(object myObj)
{
if (myObj.ToString() == string.Empty)
return false;
return true;
}
}
}
XAML:
<Window x:Class="_3DP_CallBack_DefaultValue.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Label Content="Enter String:" Grid.Row="0"
VerticalAlignment="Center" />
<TextBox Text="" Name="MyTextblock" Height="25"
Width="150" HorizontalAlignment="Right" />
</Grid>
</Window>
更新:
上面是与2以前的WPF应用三维(增量)版本的WPF应用程序中,我运行没有任何错误
第二个版本有:
- 完全相同XAML代码
- 完全相同的C#
MainWindow()
的身体构造/方法; - 在
class DependencyPropertySample : DependencyObject{}
OnMyPropertyChanged( DependencyObject dObject, DependencyPropertyChangedEventArgs e)
OnValidateMyProperty(object myObj)
OnCoerceValue(DependencyObject dObject, object val)
和public static readonly DependencyProperty MyDependencyProperty = DependencyProperty.Register()
缺席方法不同:
这里是与c从工作的第二个版本的DependencyPropertySample
类颂歌:
public class DependencyPropertySample : DependencyObject
{
//Register Dependency Property
public static readonly DependencyProperty MyDependencyProperty =
DependencyProperty.Register
("MyProperty", typeof(string), typeof(DependencyPropertySample));
public string MyProperty
{
get
{
return (string)GetValue(MyDependencyProperty);
}
set
{
SetValue(MyDependencyProperty, value);
}
}
这里是DependencyPropertySample
类的从失败的应用的3D版本的代码:
public class DependencyPropertySample : DependencyObject
{
//Register Dependency Property
public static readonly DependencyProperty MyDependencyProperty
= DependencyProperty.Register
(
"MyProperty", typeof(string), typeof(DependencyPropertySample),
new PropertyMetadata
(
"Test",
new PropertyChangedCallback(OnMyPropertyChanged),
new CoerceValueCallback(OnCoerceValue)
),
new ValidateValueCallback(OnValidateMyProperty)
);
public string MyProperty
{
get
{
return (string)GetValue(MyDependencyProperty);
}
set
{
SetValue(MyDependencyProperty, value);
}
}
public static void OnMyPropertyChanged(DependencyObject dObject,
DependencyPropertyChangedEventArgs e)
{
MessageBox.Show(e.NewValue.ToString());
}
public static string OnCoerceValue(DependencyObject dObject, object val)
{
if (val.ToString().CompareTo("Test") == 1)
{
return val.ToString();
}
return string.Empty;
}
public static bool OnValidateMyProperty(object myObj)
{
if (myObj.ToString() == string.Empty)
return false;
return true;
}
}
我没有看到你在任何地方指定你想绑定的属性。使用mybinding.Path =“MyProperty” – 2013-04-26 05:17:31
@ Erti-ChrisEelmaa,'mybinding.Path =“MyPropertyPath”;'给出编译错误'“无法将源类型'字符串'转换为目标类型'System.Windows.PropertyPath''.无论如何,不清楚为什么同一个应用程序的先前版本在没有任何路径设置的情况下没有任何问题请参阅我的问题 – Fulproof 2013-04-26 05:53:26