wpf中,当DataGrid.ItemsSource与ObservableCollection绑定后,值变化时,DataGrid如何刷新?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wpf中,当DataGrid.ItemsSource与ObservableCollection绑定后,值变化时,DataGrid如何刷新?相关的知识,希望对你有一定的参考价值。

private ObservableCollection<ZDMsgFile> _zdmsgFile = new ObservableCollection<ZDMsgFile>();

DataGrid.ItemsSource = _zdmsgFile;
当在后台_zdmsgFile值变化时,DataGrid显示的内容并不变化,
在_zdmsgFile变化后,调用DataGrid的什么方法能使DataGrid的显示的数据与_zdmsgFile同步?
谢谢

参考技术A 先设置DataGrid的ItemsSource 为空,然后重新绑定:

this.DataGrid.ItemsSource = null;
this.DataGrid.ItemsSource = _zdmsgFile;本回答被提问者采纳

当列表视图中的扩展器折叠时,无法让 WPF 列表视图折叠

【中文标题】当列表视图中的扩展器折叠时,无法让 WPF 列表视图折叠【英文标题】:Can't get WPF Listview to collapse when expanders in the listview collapse 【发布时间】:2010-11-25 18:20:21 【问题描述】:

我有一个 wpf ListView,里面有两个 Expanders。当扩展器展开列表视图时,会为内容腾出空间。当它们折叠时,列表视图不会“收回”额外的空间。我已将HorizontalAlignmentVerticalAlignment 设置为Stretch。有没有办法做到这一点? DetailsChart 列包含扩展器。请参阅下面的代码。谢谢。

这里

<Window.Resources>

    <DataTemplate x:Key="StockPriceChangeCell">
        <StackPanel Orientation="Horizontal">
            <Label Name="stockPriceChangeValue" Foreground="Binding StockPriceChangeColor" Content="Binding StockPriceChange" Width="Auto"></Label>
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="DetailsCell">
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
            <Expander Header="Details..." BorderBrush="DarkBlue" Width="200" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <Grid Name="stockDetailGrid" Background="Beige" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ShowGridLines="False">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="96*" />
                        <RowDefinition Height="6*" />
                        <RowDefinition Height="96*" />
                        <RowDefinition Height="6*" />
                        <RowDefinition Height="96*" />
                        <RowDefinition Height="6*" />
                        <RowDefinition Height="96*" />
                        <RowDefinition Height="6*" />
                        <RowDefinition Height="96*" />
                        <RowDefinition Height="96*" />
                        <RowDefinition Height="96*" />
                        <RowDefinition Height="6*" />
                        <RowDefinition Height="96*" />
                        <RowDefinition Height="6*" />
                        <RowDefinition Height="96*" />
                        <RowDefinition Height="6*" />
                        <RowDefinition Height="96*" />
                        <RowDefinition Height="6*" />
                        <RowDefinition Height="96*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Label Grid.Row="0" Grid.Column="0" Name="previousClosePrompt" FontWeight="Bold" FontSize="12" Height="28" Width="Auto">Prev Close:</Label>
                    <Label Grid.Row="0" Grid.Column="1" Name="previousCloseValue" FontWeight="Bold" FontSize="12" Height="28" Width="Auto" Content="Binding StockDetails.PreviousClose"></Label>
                    <Separator Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator3" Margin="0,2" />
                    <Label Grid.Row="2" Grid.Column="0" Name="openPricePrompt" FontWeight="Bold" FontSize="12" Height="28">Open:</Label>
                    <Label Grid.Row="2" Grid.Column="1" Name="openPriceValue" FontWeight="Bold" FontSize="12" Height="28" Content="Binding StockDetails.OpeningPrice"></Label>
                    <Separator Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator5" Margin="0,2" />
                    <Label Grid.Row="4" Grid.Column="0" Name="yearlyTargetEstimatePrompt" FontWeight="Bold" FontSize="12" Height="28">1y Target Est:</Label>
                    <Label Grid.Row="4" Grid.Column="1" Name="yearlyTargetEstimateValue" FontWeight="Bold" FontSize="12" Height="28" Content="Binding StockDetails.YearTargetEstimate"></Label>
                    <Separator Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator6" Margin="0,2" />
                    <Label Grid.Row="6" Grid.Column="0" Name="stockDaysRangePrompt" FontWeight="Bold" FontSize="12" Height="28">Day's Range:</Label>
                    <Label Grid.Row="6" Grid.Column="1" Name="stockDaysRangeValue" FontWeight="Bold" FontSize="12" Height="28" Content="Binding StockDetails.DaysRange"></Label>
                    <Separator Grid.Row="7" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator7" Margin="0,2" />
                    <Label Grid.Row="8" Grid.Column="0" Name="stockYearsRangePrompt" FontWeight="Bold" FontSize="12" Height="28">52wk Range:</Label>
                    <Label Grid.Row="8" Grid.Column="1" Name="stockYearsRangeValue" FontWeight="Bold" FontSize="12" Height="28" Content="Binding StockDetails.YearsRange"></Label>
                    <Separator Grid.Row="9" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator8" Margin="0,2" />

                    <Label Grid.Row="10" Grid.Column="0" Name="averageVolumePrompt" FontWeight="Bold" FontSize="12" Height="28">Avg Vol (3m):</Label>
                    <Label Grid.Row="10" Grid.Column="1" Name="averageVolumeValue" FontWeight="Bold" FontSize="12" Height="28" Content="Binding StockDetails.AverageVolume"></Label>
                    <Separator Grid.Row="11" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator9" Margin="0,2" />
                    <Label Grid.Row="12" Grid.Column="0" Name="marketCapPrompt" FontWeight="Bold" FontSize="12" Height="28">Market Cap:</Label>
                    <Label Grid.Row="12" Grid.Column="1" Name="marketCapValue" FontWeight="Bold" FontSize="12" Height="28" Content="Binding StockDetails.MarketCap"></Label>
                    <Separator Grid.Row="13" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator10" Margin="0,2" />
                    <Label Grid.Row="14" Grid.Column="0" Name="priceEarningRatioPrompt" FontWeight="Bold" FontSize="12" Height="28">P/E (ttm):</Label>
                    <Label Grid.Row="14" Grid.Column="1" Name="priceEarningRatioValue" FontWeight="Bold" FontSize="12" Height="28" Content="Binding StockDetails.PriceEarningsRatio"></Label>
                    <Separator Grid.Row="15" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator11" Margin="0,2" />
                    <Label Grid.Row="16" Grid.Column="0" Name="earningsPerSharePrompt" FontWeight="Bold" FontSize="12" Height="28">EPS (ttm):</Label>
                    <Label Grid.Row="16" Grid.Column="1" Name="earningsPerShareValue" FontWeight="Bold" FontSize="12" Height="28" Content="Binding StockDetails.EarningsPerShare"></Label>
                    <Separator Grid.Row="17" Grid.Column="0" Grid.ColumnSpan="2" Name="stockSeparator12" Margin="0,2" />
                    <Label Grid.Row="18" Grid.Column="0" Name="dividendYieldPrompt" FontWeight="Bold" FontSize="12" Height="28">Div &amp; Yield:</Label>
                    <Label Grid.Row="18" Grid.Column="1" Name="dividendYieldValue" FontWeight="Bold" FontSize="12" Height="28" Content="Binding StockDetails.DividendYield"></Label>
                </Grid>
            </Expander>
        </StackPanel>
    </DataTemplate>

    <DataTemplate x:Key="ChartCell">
        <StackPanel>
            <Expander Header="Chart..." BorderBrush="DarkBlue" Width="200" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                <Image Name="stockGraph" Stretch="None" Source="Binding StockChart"/>
            </Expander>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

这是列表视图:

                <TabItem Name="myStocksTab" Header="My Stocks" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                    <StackPanel Name="myStocksTabContainerPanel" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
                        <Label Name="myStocksPrompt" Foreground="GhostWhite" Background="DarkBlue" FontSize="16" FontWeight="Bold">My Stocks</Label>
                            <ListView Name="stocksUIList" ItemsSource="Binding" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" VerticalContentAlignment="Stretch">
                                <ListView.View>
                                    <GridView>
                                        <GridViewColumn Header="Action" CellTemplate="StaticResource ButtonsCell" Width="Auto"/>
                                        <GridViewColumn Header="Company/Stock Symbol" Width="Auto" DisplayMemberBinding="Binding CompanyName"/>
                                        <GridViewColumn Header="Last Trade" Width="Auto" DisplayMemberBinding="Binding LastTrade"/>
                                        <GridViewColumn Header="Trade Time" Width="Auto" DisplayMemberBinding="Binding StockDetails.TradeTime"/>
                                        <GridViewColumn Header="Change" CellTemplate="StaticResource StockPriceChangeCell"/>
                                        <GridViewColumn Header="Volume" Width="Auto" DisplayMemberBinding="Binding TradingVolume"/>
                                        <GridViewColumn Header="Details" CellTemplate="StaticResource DetailsCell" Width="215"/>
                                        <GridViewColumn Header="Chart" CellTemplate="StaticResource ChartCell" Width="215"/>
                                    </GridView>
                                </ListView.View>
                            </ListView>
                        <Button Name="addStock" FontSize="14" FontWeight="Bold" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" Click="AddCompanyStock">Add Stock...</Button>
                    </StackPanel>

【问题讨论】:

【参考方案1】:

我知道这是一篇较旧的帖子,但我一直在寻找解决此问题的方法,并希望用我的解决方案对其进行更新。

经过一番搜索,我能够使用此处找到的解决方案来修复它:ListView with nested Expander not collapsing

基本上,我在项目模板中使用了带有扩展器的 ListView。折叠扩展器时无法正确调整大小。此外,当扩展器内容大于 ListView 时,滚动将无法正常工作。

修复只是简单地将 ListView 替换为 ItemsControl 并将 ItemsControl 放入 ScrollViewer。

【讨论】:

【参考方案2】:

听起来 ListView 中可能存在某种布局错误。您是否尝试过在任一扩展器的 Collapsed 事件中调用 ListView 上的 InvalidateVisual()?这可能会奏效。

【讨论】:

同样的问题,但 InvalidateVisual() 似乎没有效果。

以上是关于wpf中,当DataGrid.ItemsSource与ObservableCollection绑定后,值变化时,DataGrid如何刷新?的主要内容,如果未能解决你的问题,请参考以下文章

当 WPF 中的绑定为空时,如何避免 xaml 警告?

WPF:仅当列表框中的一项被选中时才启用按钮

当应用程序正在执行某些任务时,Wpf 控件不可见,它在任务完成后出现

wpf 当DataGrid列模版是ComboBox时,显示信息

wpf中 定义一个button 在button的内部放置一个label,当点击label的时

当 Datagrid 失去焦点时,WPF DataGridRow 自定义样式被解除