Как привязать данные c # к списку - PullRequest
0 голосов
/ 22 сентября 2019

Я хочу создать свойство зависимости, затем привязать к этому свойству и показать данные в виде списка в форме WPF.

Просто пытаюсь понять, что я делаю.

namespace ExpenseIt
{
public class SomeData
{
    // Initialiser to help fill data
   public SomeData(string arg_firstName, string arg_lastName)
    {
        FirstName = arg_firstName;
        LastName = arg_lastName;
    }

public SomeData()
    {


    }


    // Public variables to hold data
    public string FirstName = "sean, Erika, william, ";
    public string LastName = "Turner, Purdy, gaff";

}

public class SomeDataThatWorksInXAML
{
    public void SomeData(string arg_firstName, string arg_lastName)
    {
        FirstName = arg_firstName;
        LastName = arg_lastName;
    }
    // Initialiser to help fill data
    SomeDataThatWorksInXAML(string arg_firstName, string arg_lastName)
    {
        FirstName = arg_firstName;
        LastName = arg_lastName;
    }

    // Public PROPERTIES, these are not variables but are used just like variables.  These are powerful and when accessing from XAML require properties, not variables.
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

/// <summary>
/// Interaction logic for ExpenseItHome.xaml
/// </summary>
public partial class ExpenseItHome : Page
{
    #region MyDataInCode
    public static readonly DependencyProperty MyDataInCodeProperty =
        DependencyProperty.Register
        (
            "MyDataInCode",
            typeof(bool),
            typeof(ExpenseItHome),
            new PropertyMetadata(true)
        );

    public bool MyDataInCode
    {
        get { return (bool)GetValue(MyDataInCodeProperty); }
        set { SetValue(MyDataInCodeProperty, value); }
    }
    #endregion


    public ExpenseItHome()
    {
        InitializeComponent();

        Loaded += FormIsNowLoaded;


    }

    private void FormIsNowLoaded(object sender, RoutedEventArgs e)
    {
        // Create version 1 (no initialiser)
        var aTest = new SomeData();
        aTest.FirstName = "Sean";
        aTest.LastName = "Turner";




        // In here, the panel has been made visible.  If you minimise and max, it will call this event again.
        // Play a sound

        // See if you can create a DepenedancyPMyDataInCodeproperty
        // Then BIND to this propertry: 
        // Experiment binding to the bool to show it's text, IsEnabled, 
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        // View Expense Report
        ExpenseReportPage expenseReportPage = new ExpenseReportPage(this.peopleListBox.SelectedItem);
        this.NavigationService.Navigate(expenseReportPage);

    }


}
}

Что мне нужно сделать, чтобы получить данные на передней части формы.

Внешний интерфейс выглядит следующим образом ..

<ListBox x:Name="peopleListBox" Grid.Column="1" Grid.Row="2"
             ItemsSource="{Binding MyDataInCode, ElementName=ExpenseItHomeControl}"
             ItemTemplate="{StaticResource nameItemTemplate}"/>

Насколько я могу судить о данныхпривязывается с помощью myDataInCode.

Что мне не хватает?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...