2016-02-18 33 views
1

我想要的是将添加到空白数据网格的值保存到列表中。我在C#下面的代码如何使用wpf中的绑定修改列表的值

public class HolidayCalendarView 
{ 
    public List<HolidayCalendarDaysView> HolidayCalendarDays { get; set; } 
} 

HolidayCalendarDaysView类

public class HolidayCalendarDaysView 
{ 
    public int HolidayID { get; set; } 
    public DateTime DayToPlay { get; set; } 
    public HolidayBehaviourType HolidayBehaviourType { get; set; } 
} 

我的WPF代码

<DataGrid x:Name="selectedDatesGridView" 
      Grid.Column="2" 
      Width="140" 
      CanUserReorderColumns="True" 
      Style="{StaticResource DataGridStyle}" 
      Margin="0,0,5,0"> 
    <DataGrid.Columns> 
     <DataGridTextColumn 
       Header="{x:Static resx:Resources.WordSelectedDates}" 
       Binding="{Binding DayToPlay, Mode=TwoWay, StringFormat={}{0:dd/MM/yyyy}}" 
       Width="1.5*" /> 
    </DataGrid.Columns> 
</DataGrid> 

我在窗体的构造函数设置:

this.selectedDatesGridView.ItemsSource = holidayCalendarView.HolidayCalendarDays; 

当用户点击■在日历中选择日期的日期被添加到数据网格,但没有在列表中

private void holidayCalendarDays_SelectedDatesChanged(object sender, SelectionChangedEventArgs e) 
    { 
     selectedDatesGridView.Items.Add((DateTime)holidayCalendarDays.SelectedDate); 
     selectedDatesGridView.Items.Refresh(); 
    } 

When the user clicks a date in the calendar the date is added to the datagrid but not in the list

我怎样才能使它发挥作用?感谢您的帮助!

+1

尝试使用'ObservableCollection '而不是'List ';不像'List <>','ObservableCollection'通知何时添加项目 – ASh

+0

@ASh nope没有工作,谢谢 – Relix

+0

我的想法是选择日期被添加到'HolidayCalendarDays'集合中,datagrid显示集合,日历和数据网格don直接沟通。我不得不重新阅读这个问题,并且 - '当用户点击日历中的日期时,日期被添加到数据网格中 - 这个部分是如何实现的? – ASh

回答

0

对不起,这个答案必须是一个评论。

正如@Ash在他的评论中提出的那样,使用observablecollection,需要注意的一点是,实现INotifyPropertyChanged接口,以便在UI和业务逻辑之间交换数据(希望您使用的是MVVM)。还要使用模式设置为twoway的绑定来设置ItemSource。

希望这会有所帮助。

0

您的代码将其添加到数据网格中,但不更新列表。

有一个类型为List的公共属性,并将数据网格内容绑定到它。 然后添加到该列表。它可能会工作,但你真的想使用ObservableCollection来通知变化,所以一旦集合发生变化,你就会自动看到变化(额外的提示,它只会在添加或删除项目时执行)。内的项目,你需要把自己:)刷新它的保健

我会试着给你的分

这是目前正使用MVVM光一个项目的一些代码,但这个想法是一样的。

在xaml中。请注意0​​和绑定。

<Grid ToolTip="Notice that the scrollview works inside the Grid."> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"></RowDefinition> 
     <RowDefinition Height="*"></RowDefinition> 
     </Grid.RowDefinitions> 
    <TextBlock >Example 1: ListBox with items inside a Grid: </TextBlock> 
    <ListBox Grid.Row="1" ItemsSource="{Binding MyCol}"></ListBox> 
</Grid> 

某处的一类:

public InnerControlViewModel() 
{ 
    if (IsInDesignMode) 
    { 
     MyCol = new ObservableCollection<string>() {"You are in design mode", "Next items are randomly generated:", " "}; 
    } 
    else 
    { 
     MyCol = new ObservableCollection<string>() { "You are in runtime mode", "Next items are randomly generated:", " " }; 
    } 

    for (int i = 0; i < 30; i++) 
    { 
     MyCol.Add("Random Date: " + RandomHelper.RandomDate()); 
    } 

} 

public ObservableCollection<string> MyCol { get; set; } 

注意,在构造函数中,我创建了设计师的一些假数据,并实时我得到一些随机数据。

事实上,它是一个可观察的集合,首当其冲的更新事物,并添加到它,也将刷新视图。