WPF - 将DataGrid项绑定到ListBox项

时间:2017-05-13 14:11:42

标签: c# wpf xaml datagrid listbox

我遇到了使用ListBox项绑定DataGrid项(可能是可编辑的)的问题。 我已将项目添加到DataGrid(在cs代码中),并且此项目未显示在ListBox项目中。

这是xaml代码:

<Window x:Class="_303_slawinskam_5B.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:_303_slawinskam_5B"
    mc:Ignorable="d"
    Title="Movies manager" Height="600" Width="800" MinHeight="400" MinWidth="600" Loaded="Window_Loaded">
<Grid>

    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid.Background>
        <LinearGradientBrush StartPoint="0,0.5" EndPoint="1,0.5">
            <GradientStop Color="#007c04" Offset="0.0" />
            <GradientStop Color="#b2ffb4" Offset="1.0" />
        </LinearGradientBrush>
    </Grid.Background>

    <Menu Grid.ColumnSpan="2"  HorizontalAlignment="Left" Height="25" Margin="0,0,0,0" VerticalAlignment="Top" Width="792">
        <MenuItem Header="Menu" Width="55" Height="25">
            <MenuItem Header="Help" Click="Help_Click"/>
            <MenuItem Header="Add movies" Click="Add_Click" />
            <MenuItem Header="Exit" Click="Exit_Click" />
        </MenuItem>
    </Menu>

    <TabControl Margin="10,10,0,10" Background="Transparent" Grid.RowSpan="2">
        <TabItem Header="View" Margin="716,39,-735,-47">
            <ListBox Margin="0,0,67,0" Background="Transparent" ItemsSource="{Binding ElementName=dataGridMovies}" IsSynchronizedWithCurrentItem="True">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Vertical">
                            <TextBlock Text="Title: {Binding Title}"></TextBlock>
                            <TextBlock Text="Director: {Binding Director}"></TextBlock>
                            <TextBlock Text="Score: {Binding Score}"></TextBlock>
                            <TextBlock Text="Type: {Binding Type}"></TextBlock>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </TabItem>
        <TabItem Header="Edit" Margin="679,72,-703,-81">
            <DataGrid Margin="0,6,68,0" Background="Transparent" x:Name="dataGridMovies">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Title" Binding="{Binding Title}"/>
                    <DataGridTextColumn Header="Director" Binding="{Binding Director}"/>
                    <DataGridTextColumn Header="Score" Binding="{Binding Score}"/>
                    <DataGridTextColumn Header="Type" Binding="{Binding Type}"/>
                </DataGrid.Columns>
            </DataGrid>
        </TabItem>
    </TabControl>

</Grid>

这是cs代码:

using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    namespace _303_slawinskam_5B
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            }

            private void Help_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("You won't find any help here");
            }

            private void Add_Click(object sender, RoutedEventArgs e)
            {

            }

            private void Exit_Click(object sender, RoutedEventArgs e)
            {
                this.Close();
            }

            public class Movies
            {
                public string Title { get; set; }
                public string Director { get; set; }
                public string Score { get; set; }
                public string Type { get; set; }
            }

            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                List<Movies> ml = new List<Movies>();

                dataGridMovies.DataContext = ml;

                Movies check = new Movies() { Title = "title", Director = "director", Score = "score", Type = "type" };
                dataGridMovies.Items.Add(check);
            }
        }
    }

请告诉我我需要更改什么,以便添加到DataGrid的项目也会添加到ListBox中。 谢谢。

1 个答案:

答案 0 :(得分:0)

通过在代码中执行2次小校正,它将按预期工作:

1)您必须将Path ItemsSource添加为DataGrid ItemsSource ListView <TabItem Header="View" Margin="716,39,-735,-47"> <ListBox Margin="0,0,67,0" Background="Transparent" ItemsSource="{Binding ElementName=dataGridMovies,Path=ItemsSource}" IsSynchronizedWithCurrentItem="True"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Vertical"> <TextBlock Text="Title: {Binding Title}"></TextBlock> <TextBlock Text="Director: {Binding Director}"></TextBlock> <TextBlock Text="Score: {Binding Score}"></TextBlock> <TextBlock Text="Type: {Binding Type}"></TextBlock> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </TabItem> ,如下所示。

DataGrid

方法1 --- 2)您不应使用Add方法将项​​目添加到ItemsSource,而是将DataGrid的{​​{1}}分配到列表Movies。因此,将Window_Loaded处理程序更改为

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    List<Movies> ml = new List<Movies>();

    dataGridMovies.DataContext = ml;

    Movies check = new Movies() { Title = "title", Director = "director", Score = "score", Type = "type" };
    Movies check1 = new Movies() { Title = "title1", Director = "director1", Score = "score1", Type = "type1" };
    var movies = new List<Movies> {check1, check};
    dataGridMovies.ItemsSource = movies;
}

方法2 --- 2)创建ObservableCollection Movies并将该集合分配给ItemsSource的{​​{1}}并更新集合什么时候需要。通过这种方式,您可以动态更新。

DataGrid

并从private ObservableCollection<Movies> MoviesCol { get; set; } private void Window_Loaded(object sender, RoutedEventArgs e) { List<Movies> ml = new List<Movies>(); dataGridMovies.DataContext = ml; Movies check = new Movies() { Title = "title", Director = "director", Score = "score", Type = "type" }; Movies check1 = new Movies() { Title = "title1", Director = "director1", Score = "score1", Type = "type1" }; Movies check2 = new Movies() { Title = "title2", Director = "director2", Score = "score2", Type = "type2" }; MoviesCol.Add(check1); MoviesCol.Add(check); dataGridMovies.ItemsSource = MoviesCol; MoviesCol.Add(check2); } 初始化此MovieCol属性。

constructor