2014-10-30 82 views
0

谁能告诉我为什么我需要点击两次第一次输入新的DataGrid以便我的标签更新它的内容?我试图尽可能多地删除不需要的代码。为什么我需要点击两次DataGrid来更新我的绑定

这是我的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并包含在我的SoSummaryViewModelSelectedSalesOrderViewModel设置。只要点击新的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之内。这是一张图片,这可能会让它更加清晰。 enter image description here

+0

我无法在给定的'xaml'中看到'DataGrid'。 – Sinatr 2014-10-30 15:51:23

+0

@Sinatr对不起,现在试试看 - 谢谢 – 2014-10-30 16:02:25

回答

0

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

if (_selectedSalesOrderViewModel != value) 
    ... 

而你需要点击两次,因为你的第一个点击不改变DataGrid选择(我认为)。此外,如果你扩展了2个项目(这有可能吗?),它将会非常奇怪地工作,因为将有两个DataGrids绑定到相同的项目;如果有10个项目,第10个被选中,那么第二个如何处理呢?

尝试使用OneWay绑定LabelOneWayToSource对于DataGrid

P.S .:我不确定这是否是正确的答案,但这太多了,无法评论。

相关问题