繁体   English   中英

为什么我需要在DataGrid上单击两次以更新绑定

[英]Why do I need to click twice on DataGrid to update my Binding

谁能告诉我为什么我第一次输入新的DataGrid时需要单击两次以便我的Label更新它的Content? 我试图切出尽可能多的不必要的代码。

这是我的XAML

<Window x:Class="PSS.LateOrderPredictor.Ui.View.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:PSS.LateOrderPredictor.Ui.ViewModel"
    Title="MainWindow" Height="600" Width="1350
    " WindowState="Maximized">
<Window.Resources>
    ...
</Window.Resources>
<Window.DataContext>
    <vm:SoSummaryViewModel/>
</Window.DataContext>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="25"/>
        <ColumnDefinition Width="800"/>
        <ColumnDefinition Width="500"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <ListBox ItemsSource="{Binding Path=SummaryLineItems}" 
             SelectedItem="{Binding Path=SelectedSalesOrderViewModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
             ...
             >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Expander HorizontalAlignment="Stretch"
                          MaxHeight="500">
                    <Expander.Header>
                        ...
                    </Expander.Header>
                    <DataGrid ItemsSource="{Binding Path=LineItems}" 
                              SelectedItem="{Binding Path=SelectedDemandEvent,     UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                          ...
                          />                        
                      </Expander>
                  </DataTemplate>
                </ListBox.ItemTemplate>
    </ListBox>
    <Label Grid.Column="2"
           Grid.Row="0"
           <!--Here is the label I am trying to set-->
           DataContext="{Binding Path=SelectedSalesOrderViewModel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
           Content="{Binding SelectedDemandEvent.PartNumber}"/>
           ...
    </Grid>
</Grid>

当我单击DataGridRow时,将设置SelectedSalesOrderViewModel ,并将其包含在我的SoSummaryViewModel类中。 每当我单击一个新的DataGrid时,就会在第一次单击时设置它。

public class SoSummaryViewModel : ViewModelBase
{
    ...
    /// <summary>
    /// The view model for the grid that contains the SelectedSalesOrderLine
    /// </summary>
    public SoSummaryLineViewModel SelectedSalesOrderViewModel
    {
        get { return _selectedSalesOrderViewModel; }
        set 
        {
            if (_selectedSalesOrderViewModel != value)
            {
                _selectedSalesOrderViewModel = value;
                OnPropertyChanged("SelectedSalesOrderViewModel");
            }
        }
    }      
}

在最初单击两次后(只需单击两次,而不是实际的双击事件),只要我保持在同一DataGrid ,标签将在所选行更改时随时更新。 我正在使用内部带有DataGrid的扩展器,这些扩展器位于ListBox 这是一张图片,这可能会使它更加清晰。 在此处输入图片说明

就个人而言,我不会这样做

if (_selectedSalesOrderViewModel != value)
    ...

而且您需要两次单击,因为第一次单击不会更改DataGrid选择(我假设)。 而且,如果您扩展了2个项目(可能吗?),它将非常奇怪地工作,因为将有两个DataGrids绑定到同一项目。 如果一个有10个项目并且选择了第10个,那么只有5个项目的第二个应该如何处理呢?

尝试使用OneWay绑定LabelOneWayToSourceDataGrid

PS:我不确定这是否是正确的答案,但是对于评论来说太过分了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM