wpf我的UI不重新刷新

wpf我的UI不重新刷新,wpf,wpf-controls,binding,Wpf,Wpf Controls,Binding,我的xml是: <Window.Resources> <Style TargetType="ListViewItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> </Style> </Window.Resources> <Grid > <Grid.ColumnDefinitions&g

我的xml是:

<Window.Resources>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</Window.Resources>


<Grid  >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="381*" />
        <ColumnDefinition Width="20*" />
        <ColumnDefinition Width="101*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="110*" />
        <RowDefinition Height="201*" />
    </Grid.RowDefinitions>
    <StackPanel Margin="320,0,0,0" Grid.RowSpan="2">
        <ListView ItemsSource="{Binding employeeCollection}">
            <ListView.View>
                <GridView>

                    <GridViewColumn Header="Employee ID" DisplayMemberBinding="{Binding Path=EmployeeID}"/>
                    <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=FirstName}"/>
                    <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=LastName}"/>
                    <GridViewColumn Header="start" DisplayMemberBinding="{Binding Path=startHR}"/>
                    <GridViewColumn Header="finish" DisplayMemberBinding="{Binding Path=finishHR}">

                </GridViewColumn>
            </GridView>
    </ListView.View>

        </ListView>
    </StackPanel>
            <StackPanel Margin="2,0,0,137" Grid.RowSpan="2" Grid.ColumnSpan="2" Grid.Column="1">
        <ListBox FontFamily="Guttman Yad-Brush" BorderBrush="AliceBlue" BorderThickness="5" ItemsSource="{Binding Path=dateItems}" DisplayMemberPath="Name" SelectedValuePath="Name" SelectedValue="{Binding Path=dateItem}" Width="233" Height="164" />
    </StackPanel>
    <Button Click="Button_Click" Width="102" Height="34" Margin="0,98,-1,69" Grid.Row="1" Grid.Column="2" Content="בחר" FontFamily="Guttman Yad-Brush" Background="AliceBlue"></Button>
    <TextBox Name="dateTextBox" Grid.Column="1" Margin="26,152,0,33" Grid.Row="1" FontFamily="Guttman Yad-Brush" Grid.ColumnSpan="2" />
    <Calendar SelectedDate="{Binding Path=SelectedDate}" Height="168" Name="calendar1" Width="182" SelectedDatesChanged="calendar1_SelectedDatesChanged" Margin="66,68,485,115" Grid.RowSpan="2" />
</Grid>


--> --> 这是开始窗口类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows.Data;

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
{
    ConnectionViewModel vm;

    public MainWindow()
{
    InitializeComponent();
    vm = new ConnectionViewModel();

    DataContext = vm;
}
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        //((ConnectionViewModel)DataContext).dateItems = "test";
        if (vm.cm.dateItem.ToString() != null)
        {
            dateTextBox.Text = vm.cm.dateItem.ToString();
            vm.em.insert();
        }
    }

    private void calendar1_SelectedDatesChanged(object sender, SelectionChangedEventArgs e)
    {

            string []s = calendar1.SelectedDate.ToString().Split(' ');
            dateTextBox.Text = s[0];
    }

 }

public class ConnectionViewModel
{

    public DateConectionModule cm;
    public employeesGrid em;

    public ConnectionViewModel()
    {

        cm = new DateConectionModule();
        em = new employeesGrid();

    }

    public CollectionView dateItems
    {
        get { return cm.dateItems; }
    }
    public string dateItem
    {
        get {return cm.dateItem;} 
        set{ cm.dateItem = value;}
    }
    public CollectionView employeeCollection
    {
        get { return em.employeeCollection; }
    }



}




public class DateConectionModule : INotifyPropertyChanged
{

    public static string[] datesString = { "01.01.2011", "02.01.2011", "03.01.2011", "04.01.2011", "05.01.2011" };

    public DateConectionModule()
    {

        employeesGrid em = new employeesGrid();
        IList<dateItem> list = new List<dateItem>();
        //query to database should be here
        foreach (string dataString in datesString)
        {
            list.Add(new dateItem(dataString));
        }
        _dateItemList = new CollectionView(list);
    }
    private readonly CollectionView _dateItemList;
    private string m_dateItem;

    public CollectionView dateItems
    {
        get { return _dateItemList; }
    }

    public string dateItem
    {
        get { return m_dateItem; }
        set
        {
            if (m_dateItem == value)
                return;
            m_dateItem = value;
            OnPropertyChanged("dateItem");
        }
    }
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}





public class dateItem
{
    public string Name { get; set; }
    public dateItem(string name)
    {
        Name = name;
    }
}

public class employeesGrid : INotifyPropertyChanged
{
    private CollectionView _dateItemList;
    private string m_dateItem;


    public employeesGrid()
    {
        IList<employiesData> list = new List<employiesData>();
        //query to database should be here
        list.Add(new employiesData{
        EmployeeID =  "036854768",
        FirstName = "yoav" ,
        LastName = "stern",
        startHR = "1600" ,
        finishHR = "0200"});
        _dateItemList = new CollectionView(list);
    }

    public void insert()
    {
        IList<employiesData> list = new List<employiesData>();
        //query to database should be here
        list.Add(new employiesData
        {
            EmployeeID = "0234235345",
            FirstName = "shoki",
            LastName = "zikri",
            startHR = "1600",
            finishHR = "0200"
        });
        _dateItemList = new CollectionView(list);
        OnPropertyChanged("employeeCollection");
    }

    public CollectionView employeeCollection
    {
        get { return _dateItemList; }

        set
        {
            if (_dateItemList == value) 
                return;
            _dateItemList = value;
            OnPropertyChanged("employeeCollection");
        }
    }
    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;

}
public class employiesData
{
    public string EmployeeID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string startHR { get; set; }
    public string finishHR { get; set; }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用系统组件模型;
使用System.Windows.Data;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间WpfApplication1
{
公共部分类主窗口:窗口
{
连接视图模型;
公共主窗口()
{
初始化组件();
vm=新连接视图模型();
DataContext=vm;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
//((ConnectionViewModel)DataContext).dateItems=“测试”;
if(vm.cm.dateItem.ToString()!=null)
{
dateTextBox.Text=vm.cm.dateItem.ToString();
vm.em.insert();
}
}
私有无效日历1\u SelectedDatesChanged(对象发送者,SelectionChangedEventArgs e)
{
字符串[]s=calendar1.SelectedDate.ToString().Split(“”);
dateTextBox.Text=s[0];
}
}
公共类连接视图模型
{
公共数据连接模块cm;
公共雇员集团;
公共连接视图模型()
{
cm=新的日期连接模块();
em=新员工网格();
}
公共集合查看日期项
{
获取{return cm.dateItems;}
}
公共字符串日期项
{
获取{return cm.dateItem;}
设置{cm.dateItem=value;}
}
公共集合查看员工集合
{
获取{return em.employeeCollection;}
}
}
公共类日期连接模块:INotifyPropertyChanged
{
公共静态字符串[]日期字符串={“01.01.2011”、“02.01.2011”、“03.01.2011”、“04.01.2011”、“05.01.2011”};
公共数据连接模块()
{
employeesGrid em=新employeesGrid();
IList list=新列表();
//对数据库的查询应该在这里
foreach(日期字符串中的字符串数据字符串)
{
添加(新的日期项(数据字符串));
}
_dateItemList=新集合视图(列表);
}
private readonly CollectionView_dateItemList;
私有字符串m_dateItem;
公共集合查看日期项
{
获取{return\u dateItemList;}
}
公共字符串日期项
{
获取{return m_dateItem;}
设置
{
如果(m_dateItem==值)
返回;
m_dateItem=值;
OnPropertyChanged(“日期项”);
}
}
私有void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
公共事件属性更改事件处理程序属性更改;
}
公共类日期项
{
公共字符串名称{get;set;}
公共日期项(字符串名称)
{
名称=名称;
}
}
公共类employeesGrid:InotifyProperty已更改
{
private CollectionView_dateItemList;
私有字符串m_dateItem;
公共雇员网格()
{
IList list=新列表();
//对数据库的查询应该在这里
列表.添加(新员工数据){
EmployeeID=“036854768”,
FirstName=“yoav”,
LastName=“stern”,
startHR=“1600”,
finishHR=“0200”});
_dateItemList=新集合视图(列表);
}
公开作废插入()
{
IList list=新列表();
//对数据库的查询应该在这里
列表.添加(新员工数据)
{
EmployeeID=“0234235345”,
FirstName=“shoki”,
LastName=“zikri”,
startHR=“1600”,
finishHR=“0200”
});
_dateItemList=新集合视图(列表);
关于财产变更(“员工收款”);
}
公共集合查看员工集合
{
获取{return\u dateItemList;}
设置
{
如果(_dateItemList==值)
返回;
_dateItemList=值;
关于财产变更(“员工收款”);
}
}
私有void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
公共事件属性更改事件处理程序属性更改;
}
公共类EmployeesData
{
公共字符串EmployeeID{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串startHR{get;set;}
公共字符串finishHR{get;set;}
}
}

1.我希望调用insertTest时,UI将加载我的新值

2.这是我的第一部wpf作品,所以关于如何使作品更可读、更高效、更简单,以及关于我糟糕的体系结构的注释,我知道这很糟糕,可以在我的观点下面提供一些建议吗

1-ConnectionViewModel类的用途是什么,它只是扭曲DataConnectionViewModel,因此我建议您可以摆脱ConnectionViewModel()并使用DataConnectionViewModel

2-我认为您可以去掉employeesGrid类,因为您只需要一个员工集合,所以与其使用seprate集合类,不如在DataConnectionViewModel()类中创建一个ObservarableCollection

3-使用Wpf-Model-View ViewModel模板这将为您提供更好的想法

4-我刚刚重新编写了您的代码,并创建了一个类似的应用程序,它使用MVVM和ObservableCollection,使用起来更简单

<Window.Resources>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
    </Style>
</Window.Resources>


<Grid  >
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="381*" />
        <ColumnDefinition Width="20*" />
        <ColumnDefinition Width="101*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="110*" />
        <RowDefinition Height="201*" />
    </Grid.RowDefinitions>
    <StackPanel Margin="320,0,0,0" Grid.RowSpan="2">
        <ListView ItemsSource="{Binding EmpList}">
            <ListView.View>
                <GridView>

                    <GridViewColumn Header="Employee ID" DisplayMemberBinding="{Binding Path=EmployeeID}"/>
                    <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=FirstName}"/>
                    <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=LastName}"/>
                    <GridViewColumn Header="start" DisplayMemberBinding="{Binding Path=startHR}"/>
                    <GridViewColumn Header="finish" DisplayMemberBinding="{Binding Path=finishHR}">

                    </GridViewColumn>
                </GridView>
            </ListView.View>

        </ListView>
    </StackPanel>
    <StackPanel Margin="2,0,0,137" Grid.RowSpan="2" Grid.ColumnSpan="2" Grid.Column="1">
        <ListBox FontFamily="Guttman Yad-Brush" BorderBrush="AliceBlue" BorderThickness="5" ItemsSource="{Binding Path=dateItems}" DisplayMemberPath="Name" SelectedValuePath="Name" SelectedValue="{Binding Path=dateItem}" Width="233" Height="164" />
    </StackPanel>
    <!--<Button Click="Button_Click" Width="102" Height="34" Margin="0,98,-1,69" Grid.Row="1" Grid.Column="2" Content="בחר" FontFamily="Guttman Yad-Brush" Background="AliceBlue"></Button>-->
    <TextBox Name="dateTextBox" Grid.Column="1" Margin="26,152,0,33" Grid.Row="1" FontFamily="Guttman Yad-Brush" Grid.ColumnSpan="2" />
    <!--<Calendar SelectedDate="{Binding Path=SelectedDate}" Height="168" Name="calendar1" Width="182" SelectedDatesChanged="calendar1_SelectedDatesChanged" Margin="66,68,485,115" Grid.RowSpan="2" />-->
</Grid>
    #region " Constructor "

    public DateConectionModule()
    {
        CreateEmployeeData();
    }
    #endregion " Constructor "


    #region " Public Properties "

    public ObservableCollection<EmployeeData> EmpList { get; set; }



    #endregion " Public Properties "


    #region " Helper Methods "

    private void CreateEmployeeData()
    {
        EmpList = new ObservableCollection<EmployeeData>();
        EmpList.Add
            (
             new EmployeeData() {  EmployeeID="1", LastName="Gates", FirstName="Bill", finishHR="", startHR =""  }
            );

    }

    #endregion " Helper Methods "




} 
    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;

        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}
        this.DataContext = new DateConectionModule();
    }
}