Я новичок в WPF и stackoverflow.
Я получил шаблон с пустым текстом. Дано 12 пробелов, но с 1 строкой я не могу получить доступ к вар. Связывание с DataContext.
Я добавил пустой комментарий в качестве комментария, единственная проблема - 7 и 8 в xaml-файле.
Запрещено изменять любой другой код в качестве 12 пробелов. .
Могу ли я иметь ошибку на других пробелах, но кроме метки C (пустые 7 и 8) она работает хорошо.
Thannk
Пробовал в пустом 7:MyObject myObject
blank 8: MyField MyObject.MyField
7 и 8 в различных комбинациях, много использовал Google, но не смог выяснить мою проблему с путем к «myField»
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace DataBinding
{
/// <summary>
/// Code behind of MainWindow.xaml
///
/// The code of this particular project is intentionally left incomplete
/// (code-behind as well as XAML). Missing pieces of code are indicated by
/// markers such as ___1___.
///
/// As an exercise, the missing pieces of code should be identified and
/// inserted. Do not add additional code. The full program should use data
/// bindings in order to display 9876543 in each Label once the button
/// Change is clicked.
/// </summary>
public partial class MainWindow : Window
{
// Create an instance of class student
protected MyClass myObject = new MyClass();
public MyClass MyObject
{
get { return myObject; }
set { myObject = value; }
}
public MainWindow()
{
InitializeComponent();
Binding myBinding = new Binding(nameof(MyClass.MyField)); // Binding myBinding = new Binding(___1___);
myBinding.Source = myObject; // myBinding.Source = ___2___;
A.SetBinding(Label.ContentProperty, myBinding); // A.SetBinding(___3___, myBinding);
D.DataContext = myObject; // D.DataContext = ___4___;
// Tried to fiqure out the root of the Problem
Console.WriteLine(C.GetBindingExpression(Label.DataContextProperty).Status);
Console.WriteLine(C.GetBindingExpression(Label.DataContextProperty).DataItem);
Console.WriteLine(C.GetBindingExpression(Label.DataContextProperty).Target);
Console.WriteLine(C.GetBindingExpression(Label.DataContextProperty).TargetProperty);
Console.WriteLine(C.GetBindingExpression(Label.DataContextProperty).ResolvedSource);
Console.WriteLine(C.GetBindingExpression(Label.DataContextProperty).ResolvedSourcePropertyName);
Console.WriteLine(C.GetBindingExpression(Label.DataContextProperty).HasError);
Console.WriteLine(C.GetBindingExpression(Label.DataContextProperty).HasValidationError);
Console.WriteLine(C.GetBindingExpression(Label.DataContextProperty).IsDirty);
Console.WriteLine(C);
Console.WriteLine(C.GetBindingExpression(Label.ContentProperty).Status);
Console.WriteLine(C.GetBindingExpression(Label.ContentProperty).DataItem);
Console.WriteLine(C.GetBindingExpression(Label.ContentProperty).Target);
Console.WriteLine(C.GetBindingExpression(Label.ContentProperty).TargetProperty);
Console.WriteLine(C.GetBindingExpression(Label.ContentProperty).ResolvedSource);
Console.WriteLine(C.GetBindingExpression(Label.ContentProperty).ResolvedSourcePropertyName);
Console.WriteLine(C.GetBindingExpression(Label.ContentProperty).HasError);
Console.WriteLine(C.GetBindingExpression(Label.ContentProperty).HasValidationError);
Console.WriteLine(C.GetBindingExpression(Label.ContentProperty).IsDirty);
}
private void Change(object sender, RoutedEventArgs e)
{
myObject.MyField = 98765432;
}
}
/// <summary>
/// A simple representation of a student's properties.
/// </summary>
public class MyClass : INotifyPropertyChanged
{
/// <summary>
/// Constructor, creates an empty object.
/// </summary>
public MyClass()
{
}
public event PropertyChangedEventHandler PropertyChanged; // public event PropertyChangedEventHandler ___12___;
/// <summary>
/// Raise a PropertyChanged event.
///
/// Should be called after a value change in one of this' properties.
/// </summary>
/// <param name="pname">A string describing the name of the property that has changed.</param>
protected void RaisePropertyChanged(string pname)
{
Console.WriteLine("RAISE " + pname);
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(pname));
}
}
int _MyField = 708312;
public int MyField
{
set
{
_MyField = value;
RaisePropertyChanged(nameof(MyClass.MyField)); // RaisePropertyChanged(___11___);
}
get
{
return _MyField;
}
}
}
}
<Window x:Class="DataBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="300" Width="300"
DataContext="{Binding RelativeSource={x:Static RelativeSource.Self}}"> <!-- DataContext="{___5___}"> -->
<StackPanel>
<Label Name="A">1234</Label>
<Label Name="B" Content="{Binding Path=MyObject.MyField}"/>
<!-- <Label Name="B" Content="{Binding ___6___}"/> -->
<Label Name="C" DataContext="{Binding Source=MyClass.MyObject}" Content="{Binding Path=MyField}"/>
<!-- <Label Name="C" DataContext="{Binding ___7___}" Content="{Binding ___8___}"/> -->
<Label Name="D" Content="{Binding Path=MyField}" />
<!-- <Label Name="D" Content="{Binding ___9___}" /> -->
<Button Click="Change">Change</Button>
<!-- <Button Click="___10___">Change</Button> -->
</StackPanel>
</Window>
При запуске программы я ожидал 4 раза 708312, только получил метки A, B и D, заполненные 708312.