Expression Blend 中的数据绑定新手

发布于 2024-09-08 19:22:18 字数 252 浏览 0 评论 0原文

我在 Expression Blend 中有一个 WPF 应用程序。

在应用程序中,我想在右侧有一个列出单词的列表框。

当用户单击一个单词时,我想在左侧显示该单词的更大文本、描述和其他一些文本。

据我所知,要列出列表框中的所有单词,我需要对包含单词、描述和其他文本的数据进行数据绑定。

我是这个数据绑定的新手。

有人可以帮我如何在 Expression Blend 4 中执行此操作吗?

谢谢!

I have a WPF application in Expression Blend.

In the application I want to have a listbox on the right side that lists words.

When the user clicks on a word, I want to appear on the left a bigger text of the word, description, and some other text.

I understand that to list all the words in the listbox I need to databind data that contains the words, description and other text.

I'm new to this data binding.

Can someone please help me how to do this in Expression Blend 4?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一念一轮回 2024-09-15 19:22:18

我构建了一个非常基本的 MVVM 示例,它将向您展示一些基本功能。

我首先创建了一个“汽车”对象。这就是我的列表框将包含的内容。

Car.cs

public class Car
{
    public Car(string make, string model, int year)
    {
        Model = model;
        Make = make;
        Year = year;
    }

    public string Model { get; set; }
    public string Make { get; set; }
    public int Year { get; set; }
}

接下来,我创建了一个 ViewModel 以设置为视图的 DataContext。 ViewModel 有两个属性:“Cars”和“SelectedCar”。视图将绑定到这两个属性。这是 ViewModel 的实现。

MainWindow_ViewModel.cs

public class MainWindow_ViewModel : INotifyPropertyChanged
{
    public MainWindow_ViewModel()
    {
        this.Cars = LoadInitialCarList();
        this.SelectedCar = this.Cars.First();
    }

    public List<Car> Cars
    {
        get { return cars; }
        set
        {
            if (cars != value)
            {
                cars = value;
                OnPropertyChanged("Cars");
            }
        }
    }
    private List<Car> cars;

    public Car SelectedCar
    {
        get { return selectedCar; }
        set
        {
            if (selectedCar != value)
            {
                selectedCar = value;
                OnPropertyChanged("SelectedCar");
            }
        }
    }
    private Car selectedCar;

    private List<Car> LoadInitialCarList()
    {
        List<Car> list = new List<Car>();

        list.Add(new Car("Chevrolet", "Camaro", 1968));
        list.Add(new Car("Ford", "Mustang", 1965));
        list.Add(new Car("Pontiac", "GTO", 1970));

        return list;
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

现在,在视图中,我能够创建一个 ListBox(就像您建议的那样)并用我的汽车列表填充它的 ItemSource。我还将 ListBox 的 SelectedItem 绑定到 ViewModel 上的 SelectedCar。我在视图中放置了一些其他文本,以显示有关所选汽车的更多数据。

MainWindow.xaml

<Window x:Class="ListBoxBinding.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"
    xmlns:local="clr-namespace:ListBoxBinding"
    >

<Window.DataContext>
    <local:MainWindow_ViewModel/>
</Window.DataContext>

<StackPanel>
    <ListBox ItemsSource="{Binding Cars}" SelectedItem="{Binding SelectedCar, Mode=TwoWay}"/>

    <StackPanel Orientation="Horizontal">
        <TextBlock Margin="3" Text="{Binding SelectedCar.Year}"/>
        <TextBlock Margin="3" Text="{Binding SelectedCar.Make}"/>
        <TextBlock Margin="3" Text="{Binding SelectedCar.Model}"/>
    </StackPanel>
</StackPanel>

当您在 ListBox 中选择一个项目时,它将更新 ViewModel 上的 SelectedItem,因此,它会更新绑定到 SelectedCar 上的属性的 TextBlock。

我认为这与您想要实现的目标类似。希望这有帮助!

I built a very basic MVVM example that will show you some of the basic functionality.

I first created a 'Car' object. This is what my listbox will contain.

Car.cs

public class Car
{
    public Car(string make, string model, int year)
    {
        Model = model;
        Make = make;
        Year = year;
    }

    public string Model { get; set; }
    public string Make { get; set; }
    public int Year { get; set; }
}

Next, I created a ViewModel to set as the DataContext of my view. The ViewModel has two properties, 'Cars' and 'SelectedCar'. The view will bind to these two properties. Here's the implementation of the ViewModel.

MainWindow_ViewModel.cs

public class MainWindow_ViewModel : INotifyPropertyChanged
{
    public MainWindow_ViewModel()
    {
        this.Cars = LoadInitialCarList();
        this.SelectedCar = this.Cars.First();
    }

    public List<Car> Cars
    {
        get { return cars; }
        set
        {
            if (cars != value)
            {
                cars = value;
                OnPropertyChanged("Cars");
            }
        }
    }
    private List<Car> cars;

    public Car SelectedCar
    {
        get { return selectedCar; }
        set
        {
            if (selectedCar != value)
            {
                selectedCar = value;
                OnPropertyChanged("SelectedCar");
            }
        }
    }
    private Car selectedCar;

    private List<Car> LoadInitialCarList()
    {
        List<Car> list = new List<Car>();

        list.Add(new Car("Chevrolet", "Camaro", 1968));
        list.Add(new Car("Ford", "Mustang", 1965));
        list.Add(new Car("Pontiac", "GTO", 1970));

        return list;
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}

Now, in the view, I was able to create a ListBox (like you suggested) and populate it's ItemSource with my list of Cars. I also bound the SelectedItem of the ListBox to the SelectedCar on the ViewModel. I placed some other text in the View to show more data about the selected Car too.

MainWindow.xaml

<Window x:Class="ListBoxBinding.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"
    xmlns:local="clr-namespace:ListBoxBinding"
    >

<Window.DataContext>
    <local:MainWindow_ViewModel/>
</Window.DataContext>

<StackPanel>
    <ListBox ItemsSource="{Binding Cars}" SelectedItem="{Binding SelectedCar, Mode=TwoWay}"/>

    <StackPanel Orientation="Horizontal">
        <TextBlock Margin="3" Text="{Binding SelectedCar.Year}"/>
        <TextBlock Margin="3" Text="{Binding SelectedCar.Make}"/>
        <TextBlock Margin="3" Text="{Binding SelectedCar.Model}"/>
    </StackPanel>
</StackPanel>

When you select an item in the ListBox, it will update the SelectedItem on the ViewModel, therefore, it updates the TextBlocks that are bound to the properties on the SelectedCar.

I think this is something similar to what you are trying to accomplish. Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文