Я не могу получить привязку данных к пользовательскому интерфейсу.Databinding не работает в WPF
Вопросы
Должен ли я реализовать DependencyProperty ИЛИ INotifyPropertyChanged? Его простая привязка данных , и я думаю, что мне не нужно использовать вышеуказанные свойства.
XMAL КОД
<Window x:Class="Pr.child"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:Pr"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
Title="" Height="327" Width="683" WindowStartupLocation="CenterOwner">
<Window.Resources>
<ObjectDataProvider x:Key="Obj" ObjectType="{x:Type src:test}" MethodName="getinfo" />
</Window.Resources>
<Label x:Name="lblTest" Content="{Binding Source={StaticResource Obj},diag:PresentationTraceSources.TraceLevel=High}"</Label>
<Button Name="btnOK" Click="btnOK_Click">OK</Button>
код За
классГлавная Окна
public partial class Window1 : Window
{
child w = new child();
}
дочернего окна класса
public partial class child : Window
{
public child()
{
InitializeComponent();
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
RunTest runTest = new RunTest();
}
}
RunTest RunTest
class RunTest
{
public RunTest()
{
test t = new test();
t.info = "test info";
t.getinfo();
}
}
испытаний Класс
class test
{
public string info { get; set; }
public string getinfo()
{
return info;
}
}
Debug Info
System.Windows.Data Warning: 52 : Created BindingExpression (hash=44780731) for Binding (hash=63630067)
System.Windows.Data Warning: 54 : Path: ''
System.Windows.Data Warning: 56 : BindingExpression (hash=44780731): Default mode resolved to OneWay
System.Windows.Data Warning: 57 : BindingExpression (hash=44780731): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 58 : BindingExpression (hash=44780731): Attach to System.Windows.Controls.Label.Content (hash=1867017)
System.Windows.Data Warning: 63 : BindingExpression (hash=44780731): Resolving source
System.Windows.Data Warning: 66 : BindingExpression (hash=44780731): Found data context element: <null> (OK)
System.Windows.Data Warning: 73 : BindingExpression (hash=44780731): Use Data from ObjectDataProvider (hash=16906910)
System.Windows.Data Warning: 74 : BindingExpression (hash=44780731): Activate with root item <null>
System.Windows.Data Warning: 100 : BindingExpression (hash=44780731): Replace item at level 0 with <null>, using accessor {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 97 : BindingExpression (hash=44780731): GetValue at level 0 from <null> using <null>: <null>
System.Windows.Data Warning: 76 : BindingExpression (hash=44780731): TransferValue - got raw value <null>
System.Windows.Data Warning: 85 : BindingExpression (hash=44780731): TransferValue - using final value <null>
Ваш код не имеет никакого смысла ... класс 'RunTest' просто создает экземпляр' test', который никогда не используется нигде, почему вы ожидаете, что 'ObjectDataProvider' будет использовать этот конкретный экземпляр' test'? Он просто создаст новый экземпляр ... Чего вы пытаетесь достичь? –