我有一個包含一個DataGrid WPF應用程序。我有一個名爲OrderBlock的對象,其中包含一個名爲Orders的列表。這個列表綁定到我的數據網格。WPF Datagrid的結合問題
我的問題是一個用戶導入其讀入我的名單「訂單」訂單列表。這工作正常& datagrid顯示所有的訂單(讓我們說總共10個訂單)。用戶然後點擊一個按鈕發送訂單。然後應用程序從外部應用程序等待,看看是否有任何訂單有問題。
比方說,一個爲了有一個問題,但其他9個已經從我的名單中刪除。我的數據網格仍然顯示所有10個訂單?除了在我的用戶界面上更新的列表以外,OrderBlock還有其他屬性,不明白爲什麼列表不是。
如果我逐句通過我的代碼,並停止在我的名單的制定者,它的工作「OnPropertyChanged」。其他
有一點需要指出的是,這有一個問題一個訂單有一個名爲RejectReason屬性,它確實從零到「一些錯誤」的變化。 Confussed。
編輯 - 代碼OrderBlock對象添加
兩個屬性 - OrderCountSuccess沒有更新列表中沒有,除非調試。
public int OrdersCountSuccess
{
get { return _ordersCountSuccess; }
set { _ordersCountSuccess = value; OnPropertyChanged("OrdersCountSuccess"); }
}
public List<Order> Orders
{
get { return _orders; }
set { _orders = value; OnPropertyChanged("Orders"); }
}
OrderBlock對象(在我的視圖模型),它包含訂單
public OrderBlock OrderBlockEntity
{
get
{
return _orderBlockEntity;
}
set
{
_orderBlockEntity = value;
OnPropertyChanged("OrderBlockEntity");
}
}
這種方法的列表,順序讀取到我的列表中,然後成功地顯示在我的datagird
private void ImportRun()
{
OrderBlockEntity = Qoe.GetOrders(_fileLocation);
}
這種方法是在命令回來跟任何錯誤信息& DataGrid中沒有更新,雖然我可以看到列表已經改變
private void SendRun()
{
OrderBlockEntity = Qoe.SendOrders(OrderBlockEntity);
}
的XAML
<!-- Grid that contains the DataGrid which shows the list of orders -->
<Grid Grid.Row="2" x:Name="GridOrders">
<!-- The data grid to display orders-->
<DataGrid DataContext="{Binding OrderBlockEntity}"
x:Name="dataGridOrders"
ItemsSource="{Binding Orders}"
Style="{StaticResource DataGridTemplate}"
ColumnHeaderStyle="{StaticResource DG_ColumnHeader}"
RowStyle="{StaticResource DG_Row}"
CellStyle="{StaticResource DG_Cell}"
RowDetailsTemplate="{StaticResource DG_RowDetail}"
RowHeaderStyle="{StaticResource DG_RowHeader}"
AutoGenerateColumns="False"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="Silver"
RowHeaderWidth="30"
Margin="25,5,20,15"
RowDetailsVisibilityChanged="dataGridOrders_RowDetailsVisibilityChanged">
<DataGrid.RowHeaderTemplate>
<DataTemplate>
<ToggleButton x:Name="RowHeaderToggleButton"
Click="RowHeaderToggleButton_Click"
Cursor="Hand"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding DataContext.MultiID, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridRow}}}" Value="False">
<Setter TargetName="RowHeaderToggleButton" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGrid.RowHeaderTemplate>
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Action">
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.StatusList}"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="SelectedValue" Value="{Binding StatusGood}"/>
<Setter Property="Background" Value="Silver"/>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=DataContext.StatusList}"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="SelectedValue" Value="{Binding StatusGood, UpdateSourceTrigger=PropertyChanged}"/>
<Setter Property="Background" Value="Silver"/>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<DataGridTextColumn Header="Fund" Binding="{Binding Account}" IsReadOnly="True"/>
<DataGridTextColumn Header="Security ID" Binding="{Binding Security.ID}" IsReadOnly="True"/>
<DataGridTextColumn Header="ThinkFolio Security ID" Binding="{Binding Security.IDThinkFolio}" IsReadOnly="True"/>
<DataGridTextColumn Header="Security Name" Binding="{Binding Security.Name}" IsReadOnly="True"/>
<DataGridTextColumn Header="Buy/Sell" Binding="{Binding TransType}" IsReadOnly="True"/>
<DataGridTextColumn Header="Quantity" Binding="{Binding OrderQunatity, StringFormat=\{0:N0\}}" IsReadOnly="False"/>
<DataGridTextColumn Header="Currency" Binding="{Binding Security.Currency}" IsReadOnly="False"/>
<DataGridTextColumn Header="Manager" Binding="{Binding FundManager}" IsReadOnly="True"/>
<DataGridTextColumn Header="Dealing Desk" Binding="{Binding Dealer}" IsReadOnly="False"/>
<DataGridTextColumn Header="Order Reason" Binding="{Binding OrderReason}" IsReadOnly="False"/>
<DataGridTextColumn Binding="{Binding RejectReason}" IsReadOnly="True">
<DataGridTextColumn.Header>
<TextBlock Text="{Binding DataContext.ColumnHeadInfo, RelativeSource={RelativeSource AncestorType={x:Type local:MainWindow}}}"/>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Header="Comments" Binding="{Binding Comments}" IsReadOnly="False" Width="*"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
據我所見,你的OrderBlock不會觸發,因爲它沒有實現INotifyCollectionChanged - 因爲OrderBlock是你的Orders集合,它應該實現這個。儘管如此,我認爲你可以用ObservableCollection更好,請查看@ user1729313答案! :) – Eisenhorn