如何清理网格内容 [英] How to clean grid content

查看:176
本文介绍了如何清理网格内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网格为:

< DockPanel Height =   280 >  
< Border CornerRadius = 6 BorderBrush = 灰色 removed = LightGray BorderThickness = 2 >
< ScrollViewer VerticalScrollBarVisibility = 自动 >
< ItemsControl Height = 400名称= icTodoList ItemsSource = {Binding Items} >
< ItemsControl.ItemTemplate> ;
< DataTemplate>
<网格名称= ToDoList >
< Grid.RowDefinitions>
< RowDefinition Height = * />
< RowDefinition Height = * />
< RowDefinition Height = * />
< RowDefinition Height = * />
< / Grid.RowDefinitions >
< TextBlock Text = {Binding StartTime,FallbackValue =''} Grid.Row = 0 />
< TextBlock Text = {Binding ConnectedTime,FallbackValue =''}网格。 Row = 1 />
< TextBlock Text = {Binding DisconnectedTime,FallbackValue =''}网格。 Row = 2 />
< TextBlock Text = {Binding DialingResult,FallbackValue =''}网格。 Row = 3 />
< / 网格 >
< / DataTemplate >
< / ItemsControl.ItemTemplate >
< / ItemsControl >
< / ScrollViewer >
< / Border >
< / DockPanel >



点击按钮开始进程中,一些内容被添加到网格中,但是下一次新的东西被添加到旧网格之后。我不想那样;我想点击按钮后清理网格,所以布局总是显示新内容。



如何?



编辑:

我使用MVVM模式。

  public  ObservableCollection< Calls> items =  new  ObservableCollection< Calls>(); 
public ObservableCollection< Calls>商品
{
获取 {返回项; }
set
{
items = value ;
RaisePropertyChanged();
}
}



在后面的代码中:

 MainViewModel _dataContext; 
调用c = new Calls();
// blah blah
Dispatcher.BeginInvoke((Action)委托()
{
if (c!= null
_dataContext.Items.Add(c);
});

解决方案

< blockquote>请参阅此文章:重置视图模型WPF MVVM应用程序中没有代码隐藏的视图 [ ^ ]


我得到了一个简单的解决方案。

在添加之前,使用一行代码就足够了。

 _ dataContext.Items.Clear(); 


I have the grid as:

<DockPanel Height="280">
          <Border CornerRadius="6" BorderBrush="Gray" removed="LightGray" BorderThickness="2" >
              <ScrollViewer VerticalScrollBarVisibility="Auto">
                  <ItemsControl Height="400" Name="icTodoList" ItemsSource="{Binding Items}">
                      <ItemsControl.ItemTemplate>
                          <DataTemplate>
                              <Grid Name="ToDoList">
                                  <Grid.RowDefinitions>
                                      <RowDefinition Height="*"/>
                                      <RowDefinition Height="*"/>
                                      <RowDefinition Height="*"/>
                                      <RowDefinition Height="*"/>
                                  </Grid.RowDefinitions>
                                  <TextBlock Text="{Binding StartTime, FallbackValue=' '}" Grid.Row="0"/>
                                  <TextBlock Text="{Binding ConnectedTime, FallbackValue=' '}" Grid.Row="1"/>
                                  <TextBlock Text="{Binding DisconnectedTime, FallbackValue=' '}" Grid.Row="2"/>
                                  <TextBlock Text="{Binding DialingResult, FallbackValue=' '}" Grid.Row="3"/>
                              </Grid>
                          </DataTemplate>
                      </ItemsControl.ItemTemplate>
                  </ItemsControl>
              </ScrollViewer>
          </Border>
      </DockPanel>


By clicking a button to start a process, some contents are add to the grid, But next time new stuffs are appended after the old ones. I don't want that; I want to clean up the grid once I click the button, so the layout always show the new contents.

How?

EDIT:
I used MVVM pattern.

public ObservableCollection<Calls> items = new ObservableCollection<Calls>();
   public ObservableCollection<Calls> Items
   {
       get { return items; }
       set
       {
           items = value;
           RaisePropertyChanged();
       }
   }


And in the code behind:

MainViewModel _dataContext;
Calls c = new Calls();
// blah blah
 Dispatcher.BeginInvoke((Action)delegate()
        {
            if (c != null)
                _dataContext.Items.Add(c);
        });

解决方案

Please, see this article: Resetting a View Model in WPF MVVM applications without code-behind in the view[^]


I got a simple solution.
Before adding, using one line code is enough.

_dataContext.Items.Clear();


这篇关于如何清理网格内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆