0

I have strange problem. Here's some code:

XAML of first view:

<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>

Code (viemodel of first view):

//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 of second view:

<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>

Code (viemodel of second view):

private DrawingToolViewModel _drawingToolViewModel;

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

Finnaly, code of 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");
    }
 }

Problem:

In the first view i'm making some calculation, draw a rectangle, add it to Grid and add this Grid to ObservableCollection called "Grid". It shows in ItemsControl.

When i click button that open new window (this windows wraps the same drawing viewmodel) the rectangle shows in new window but disappear in first... Even when i closed second view and remake calculation and drawing the rectangle doesn't show.

Where i got wrong?

Marcin
  • 1
  • 1
    You should not have UI elements (e.g. Grids) in your model or view model. Try to find an abstract representation of what you want to visualize. Note also that a two-way binding of the ItemsSource property doesn't make sense. – Clemens Mar 12 '15 at 20:46
  • Thanks for advice about finding abstract representation. I made DataTemplate (Rectangle etc) in ItemsControl.Resourses and bind UIElement properties to appropriate viewmodel properties. Now it's works great. You're right that Two-way binding is pointless here. Thank you very much :) – Marcin Mar 13 '15 at 17:02

0 Answers0