2010-08-22 120 views
0

我的XML是:WPF我的UI犯規refrash

<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; } 
} 

}

1.I希望在insertTest被稱爲UI會載入我的新值

2,本是我的第一個WPF工作塔爾等如何使事情更易讀,effican,簡單的任何建議和關於我可憐的架構筆記我知道這是crapy會有些

+0

提供更多詳情。你如何在界面中綁定(?)這個集合? – 2010-08-22 07:58:28

+0

@victor我希望我編輯的幫助可以幫助你,而不是信息超載 版本,我只是想知道,如果有更好的意見,你會建議在我的可憐的architucture,它是我的第一個WPF應用程序,我需要建議。 – 2010-08-22 08:46:37

回答

1

下面我分

1-什麼用ConnectionViewModel類的,它只是包紮DataConnectionViewModel,所以我建議你可以擺脫ConnectionViewModel的( )並使用DataConnectionViewModel。

2我認爲你可以擺脫employeesGrid類,因爲所有你需要僱員的集合,而不是使用一個單獨的集合類,使DataConnectionViewModel()類中的可觀察集合。

3-使用Wpf-型號 - 查看 - 視圖模型模板這讓你更好的主意

4-我剛纔refacror你的代碼,並創建一個使用MVVM和的ObservableCollection一個類似的應用和更易於使用。

我的XAML

<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> 

我的代碼

1-

創建DataConnectionViewModel這是繼承ViewModelBase類。

using System; using System.Collections.Generic;使用System.Linq的 ; using System.Text; 使用Employee.Models; using System.Collections.ObjectModel;

命名空間Employee.ViewModels { 公共類DateConectionModule:ViewModelBase { #地區 「實例變量」 公共靜態字符串[] datesString = { 「01.01.2011」, 「2011年2月1日」,「2011年3月1日「,」04.01.2011「,」05.01.2011「}; #endregion 「實例變量」

#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 " 




} 

}

2- ViewModelBAse類使用System

; 使用系統。Collections.Generic; using System.ComponentModel;使用System.Linq的 ; using System.Text;使用System.Windows的 ; using System.Windows.Input;

命名空間Employee.ViewModels {/// /// 提供用於視圖模型類的常見功能 /// 公共抽象類ViewModelBase:INotifyPropertyChanged的 { 公共事件PropertyChangedEventHandler的PropertyChanged;

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

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

} 

}

3-設置在MainWindow.Xaml.cs數據上下文

公共部分類的MainView:窗口 { 公共的MainView() { 的InitializeComponent();

 this.DataContext = new DateConectionModule(); 
    } 
} 

還有很多其他事情一樣依賴注入等。但對於你的情況,你可以寫一個控制器,打電話給你的DataService給你employess列表比指定列表中的ObservableCollection。

4-我可以建議閱讀abot Prism框架,它在應用程序管理和TDD中爲您提供了非常大的靈活性。

+0

是的,我認爲這一點是設計本身,我只是不知道該怎麼做,如果你可以推薦一個有實例的發明,我會非常開心。 – 2010-08-22 11:42:24