WPF在两个视图中绑定到相同的UIElement(Grid)

时间:2015-03-12 18:08:47

标签: c# wpf xaml binding uielement

我有奇怪的问题。这是一些代码:

第一次观看的XAML:

<UserControl.DataContext>
    <vm:BendingTabViewModel/>
</UserControl.DataContext>
.......
<ItemsControl Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" ItemsSource="{Binding Path=DrawingToolViewModel.Grid, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid Width="200" Height="340" Margin="0,30,0,0"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>

代码(第一个视图的模型):

//here I wrap DrawingToolViewModel
public DrawingToolViewModel DrawingToolViewModel { get; set; }

//here is the method that open new window
public void OpenRPlacementWindow(object obj)
{
    BendingReinforcementPlacementWindowView
    bendingReinforcementPlacementWindowView = new
    BendingReinforcementPlacementWindowView();
    //here i pass the same DrawingToolViewModel to viemodel of second view
    bendingReinforcementPlacementWindowView.DataContext = bendingRPlacementWindowViewModel;
        bendingReinforcementPlacementWindowView.ShowDialog();
    }

第二种观点的XAML:

<ItemsControl Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" ItemsSource="{Binding Path=DrawingToolViewModel.Grid, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Grid.Row" Value="0"/>
                <Setter Property="Grid.Column" Value="0"/>
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid Width="200" Height="340" Margin="0,30,0,0"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>

代码(第二视图的模型):

private DrawingToolViewModel _drawingToolViewModel;

public DrawingToolViewModel DrawingToolViewModel
    {
        get { return _drawingToolViewModel; }
        set { _drawingToolViewModel = value; }
    }

Finnaly,DrawingToolViewModel的代码:

private DrawingToolModel _drawingToolModel;

public DrawingToolModel DrawingToolModel
{
    get { return _drawingToolModel; }
    set { _drawingToolModel = value; }
}

public ObservableCollection<Grid> Grid
{
    get { return DrawingToolModel.Grid; }
    set
    {
        DrawingToolModel.Grid = value;
        RaisePropertyChanged("Grid");
    }
 }

问题:

在第一个视图中,我进行了一些计算,绘制一个矩形,将其添加到网格,并将此网格添加到名为&#34; Grid&#34;的ObservableCollection中。它显示在ItemsControl中。

当我点击打开新窗口的按钮(此窗口包装相同的绘图视图模型)时,矩形显示在新窗口中但在第一个消失...即使当我关闭第二个视图并重新制作计算并绘制矩形时也没有。 t show。

我哪里错了?

0 个答案:

没有答案