我試圖運行下面附加的文章「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