繁体   English   中英

使用数据绑定,数据模板和可观察集合的C#WPF应用程序

[英]C# WPF application using data binding, data templates and observable collection

我正在使用数据绑定,数据模板和可观察的集合在C#WPF中编写程序。 我将应用程序基于http://msdn.microsoft.com/zh-cn/library/vstudio/ms771319(v=vs.90).aspx上的数据绑定演示。 由于某种原因,我无法使程序显示来自绑定的任何数据。 调试告诉我,我的Records类集合有效,但在这一行上:

records_collection_db = (CollectionViewSource)(this.Resources["records_collection_db"]);

我的'records_collection_db'变量为null,而它应该包含一些数据。 我怀疑我在xaml行中的CollectionViewSource设置不正确,但是我真的不知道如何解决它。 这是我在WPF中的第一个程序,因此我对此主题没有太多经验。 目前,我的代码和示例代码之间唯一可见的区别是我的初始窗口是...演示中的位置

代码(或至少缩短版本的代码):


记录班

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace WpfApplication1
{
    public class Record : INotifyPropertyChanged
    {
        private int Lp;
        private string group;

        public event PropertyChangedEventHandler PropertyChanged;

        public int _Lp
        {
            get { return this.Lp; }
            set { this.Lp = value; OnPropertyChanged("_Lp"); }
        }

        public string _group
        {
            get { return this.group; }
            set { this.group = value; OnPropertyChanged("_group"); }
        }


        public Record(int Lp, string group)
        {
            this.Lp = Lp;
            this.group = group;
        }

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

初始窗口

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Configuration;
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 WpfApplication1
{
    public partial class MainWindow : Window
    {                
        public MainWindow()
        {
            InitializeComponent();

            var newWindow1 = new Audyt_window();
            newWindow1.Show();
        }    
    }
}

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:src="clr-namespace:WpfApplication1" 
        xmlns:System="clr-namespace:System;assembly=Mscorlib"
        Title="MainWindow" Height="350" Width="525"
        WindowStartupLocation="CenterScreen"
        >
    <Window.Resources>
        <Style x:Key="text_style" TargetType="TextBlock">
            <Setter Property="Foreground" Value="#333333" />
        </Style>        

    </Window.Resources>    

    <Grid>
        <Button Content="Start" HorizontalAlignment="Left" Margin="36,36,0,0" VerticalAlignment="Top" Width="178" Height="93" Click="Button_Click"/>
        <Button Content="Zamknij" HorizontalAlignment="Left" Margin="202,223,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
        <Button Content="Rekordy" HorizontalAlignment="Left" Margin="318,56,0,0" VerticalAlignment="Top" Width="108" Height="52" Click="Button_Click_2" />
        <Button Content="Dodaj" HorizontalAlignment="Left" Margin="351,157,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_3"/>
    </Grid>
</Window>

操作窗口

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.ComponentModel;


namespace WpfApplication1
{
    public partial class Audyt_window : Window
    {       
        CollectionViewSource records_collection;

        private ObservableCollection<Record> records = new ObservableCollection<Record>();
        public ObservableCollection<Record> Records
        {
            get { return this.records; }
            set { this.records = value; }
        }

        public Audyt_window()
        {        
            load_temp_data();    

            InitializeComponent();
            records_collection = (CollectionViewSource)(this.Resources["records_collection"]);
        }

        private void load_temp_data()
        {
            Record rek1 = new Record(601, "Gr1", "Pro1", "Pyt1", 600001, "Kat1", false, false);
            Record rek2 = new Record(602, "Gr2", "Pro2", "Pyt2", 600002, "Kat2", false, false);
            Record rek3 = new Record(603, "Gr3", "Pro3", "Pyt3", 600003, "Kat3", false, false);
            this.Records.Add(rek1);
            this.Records.Add(rek2);
            this.Records.Add(rek3);
        }            
    }
}

<Window x:Class="WpfApplication1.Audyt_window"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Audyt" 
        xmlns:src="clr-namespace:WpfApplication1" 
        ResizeMode="NoResize"
        SizeToContent="WidthAndHeight"
        WindowStartupLocation="CenterScreen"
        >

    <Window.Resources>
        <CollectionViewSource Source="{Binding Source={x:Static Application.Current}, Path=Records}" x:Key="records_collection" />

        <Style x:Key="text_style" TargetType="TextBlock">
            <Setter Property="Foreground" Value="#333333" />
        </Style>

        <DataTemplate x:Key="Records_Template" DataType="{x:Type src:Record}">
            <Border BorderThickness="2" BorderBrush="Navy" Padding="7" Name="Record_List_Border" Margin="3" Width="345">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="20"/>
                        <ColumnDefinition Width="30"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Background="Aqua" Grid.Row="0" Grid.Column="0"
                               Name="textblock_Lp"
                               Style="{StaticResource text_style}"
                               Text="Lp: "
                               >
                    </TextBlock>
                    <TextBlock Background="Azure" Grid.Row="0" Grid.Column="1"
                               Name="datablock_Lp"
                               Style="{StaticResource text_style}"
                               Text="{Binding Path=_Lp}"
                               >
                    </TextBlock>
                    <TextBlock Background="Aqua" Grid.Row="0" Grid.Column="2"
                               Name="datablock_group"
                               Style="{StaticResource text_style}"
                               Text="{Binding Path=_group}"
                               >
                    </TextBlock>

                </Grid>
            </Border>
        </DataTemplate>

    </Window.Resources>

    <Border Padding="10">
        <Grid>

            <ListBox Name="Hits_View_List" HorizontalAlignment="Left" VerticalAlignment="Top" 
                 Height="525" Width="400" Margin="420,0,0,0" 
                 BorderThickness="2" BorderBrush="DimGray" 
                 ItemsSource="{Binding Source={StaticResource records_collection}}"             
                 >                    
            </ListBox>

        </Grid>
    </Border>
</Window>

在您的Audyt_window ,将CollectionViewSource的绑定源设置为{x:Static Application.Current} ,但是Records ObservableCollection是在您的Audyt_window中声明的

更改:

<CollectionViewSource Source="{Binding Source={x:Static Application.Current}, Path=Records}" x:Key="records_collection" />

<CollectionViewSource Source="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, Path=Records}" x:Key="records_collection" />

我认为这应该可行。

您命名您的收藏集viewsource

x:Key="records_collection"

并且您正在搜索无法匹配的records_collection_db ...

records_collection_db = (CollectionViewSource)(this.Resources["records_collection_db"]);

更改密钥或将字符串更改为正确的名称

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM