gpt4 book ai didi

c# - 如何更正此说明 WPF DependencyProperty 用法的示例?

转载 作者:行者123 更新时间:2023-11-30 20:58:19 27 4
gpt4 key购买 nike

我正在尝试运行我附在下面的文章“Dependency Properties in WPF”中的代码。

但应用程序在 SetValue(MyDependencyProperty, value); 行中断,异常(exception)情况:

System.Windows.Markup.XamlParseException   
"'' is not a valid value for property 'MyProperty'."

内部异常:

{"'The invocation of the constructor on type '_3DP_CallBack_DefaultValue.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'."}

enter image description here

我应该更改什么才能运行此应用程序?

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>

更新:

上面是 3d(增量)版本的 WPF 应用程序和 2 个以前的 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()
不同:

这是 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;
}
}

最佳答案

public static string OnCoerceValue(DependencyObject dObject, object val)
{
if (val.ToString().CompareTo("Test") == 1)
{
return val.ToString();
}
**return string.Empty;**
}

该函数比较后返回string.Empty

public static bool OnValidateMyProperty(object myObj)
{
if (myObj.ToString() == string.Empty)
**return false;**
return true;
}

然后此验证返回 false。由于验证失败,您会收到错误消息“'' 不是属性 'MyProperty' 的有效值。”对这些函数进行适当的更改。

关于c# - 如何更正此说明 WPF DependencyProperty 用法的示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16228923/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com