我在文本块中遇到数据绑定问题。
我想要做的是,只要在任何类中发生异常,我都希望在文本块中显示,但出于某种原因,它并不显示。
Main.xaml
<Window x:Class="TestingWPF.Main"
<Window.Resources>
<ObjectDataProvider x:Key="showErr" ObjectType="{x:Type local:ErrorLog}" MethodName="GetError"/>
</Window.Resources>
<Frame Name="frame1" Width="620"/>
<Button Name="button1" Click="button1_Click">
<TextBlock Name="txtBlock6" Text="{Binding Source={StaticResource showErr}}"/>
</Window>
Main.xaml.cs
namespace TestingWPF
{
public partial class Main : Window
{
private void button1_Click(object sender, RoutedEventArgs e)
{
frame1.Source =new Uri("/Page1.xaml", UriKind.Relative);
}
}
public class ErrorLog
{
private string errorInfo { get; set; }
public string GetError(string errorMessage)
{
return errorInfo =errorMessage;
}
}
}
的Page1.xaml
<Page x:Class="TestingWPF.Page1"
<Button Name="button1" Click="button1_Click">
<Button Name="button2" Click="button2_Click">
</Page>
Page1.xaml.cs
namespace TestingWPF
{
public partial class Page1 : Page
{
ErrorLog errorLog = new ErrorLog ();
private void button1_Click(object sender, RoutedEventArgs e)
{
someMethod1();
}
private void someMethod1()
{
try
{
}
catch(Expection e)
{
errorLog.GetError(e.toString());// This is not dispalying
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
someMethod2();
}
private void someMethod2()
{
Class2 class2 = new Class2();
class2.foo();
}
}
}
Class2.cs
namespace TestingWPF
{
class Class2
{
ErrorLog errorLog = new ErrorLog ();
public void foo()
{
try
{
}
catch(Expection e)
{
errorLog.GetError(e.toString());// This is not dispalying
}
}
}
}
答案 0 :(得分:0)
你在哪里实际设置错误我只看到GetError方法,它只返回传递给它的相同消息?我要么添加无参数GetError
方法,要么绑定到您正在创建的对象的实际属性,即创建一个可公开访问的属性
Error {get;set;}
在您的ErrorLog类中并在SetError方法中设置它,即
public string SetError(string errorMessage)
{
Error = errorMessage;
}