0

I am trying to make a monopoly game using the WPF, Prism and MVVM. On my View I have a grid divided into several rows and columns. Each cell is a separate game card, for example, a city.

I also have a content controls which displays my players as chips.

The task is to move my players, relative to the cells in the grid. And I need to do this from my ViewModel.(I want my ViewModel tells somehow to my View where to locate player). Just help me with approach to this problem.

My View:

            <!--bottom row-->
            <ContentControl Grid.Row="10" Grid.Column="10" Content="{Binding Cards[0]}" ContentTemplateSelector="{StaticResource CardTemplateSelector}"/>
            <ContentControl Grid.Row="10" Grid.Column="9" Content="{Binding Cards[1]}" ContentTemplateSelector="{StaticResource CardTemplateSelector}"/>
            ...

            <!--left column-->
            <ContentControl Grid.Row="9" Grid.Column="0" Content="{Binding Cards[11]}"  ContentTemplateSelector="{StaticResource CardTemplateSelector}"/>
            <ContentControl Grid.Row="8" Grid.Column="0" Content="{Binding Cards[12]}"  ContentTemplateSelector="{StaticResource CardTemplateSelector}"/>
            ...

            <!--top row-->
            <ContentControl Grid.Row="0" Grid.Column="0" Content="{Binding Cards[20]}"  ContentTemplateSelector="{StaticResource CardTemplateSelector}"/>
            ...

            <!--right column-->
            ...

            <!--player chips-->
            <ContentControl Grid.Row="10" Grid.Column="10" Content="{Binding Players[0]}"  ContentTemplate="{StaticResource PlayerTemplate1}"/>
            ...

What I expect: 1)Model makes a dice roll(actually i have done this) 2)ViewModel gets this result and transform it to position on grid(have some ideas) 3)View must somehow get this position and move my player.

Eugen
  • 19
  • 5
  • You can try placing the player's content controls in each cell then use a visibility converter to only display them at their current position. – Wes Jan 03 '19 at 21:43
  • [My answer to this question](https://stackoverflow.com/a/20563181/1177147) should provide you with enough to get started. – Mark Feldman Jan 03 '19 at 22:41
  • Thank you @MarkFeldman! Can you help another one? – Eugen Jan 04 '19 at 19:26

2 Answers2

-1

Thank you MarkFeldman!Your approach helped me. But i have another problem now))

For this moment my players can move all around the game board. It's great! But, I want to simulate the passage of cards one by one, and not immediately to the final card. First I tried to make one step, thread.sleep(100) and then again step until in the right place. But this led to a complete freeze of the program until the chip was in the final card.

I would be grateful for one more hint.

Eugen
  • 19
  • 5
-1
    internal void MakeStep()
    {
        this.CardIndex++;
    }

    internal void MakeStep(int count)
    {
        Task.Factory.StartNew(() => this.StepByStep(count));

    }

    private void StepByStep(int count)
    {
        for (int i = count; i > 0; i--)
        {
            System.Windows.Application.Current.Dispatcher.BeginInvoke(new Action(() =>
            {
                this.MakeStep();
                Debug.Print(this.CardIndex.ToString());
            }), DispatcherPriority.Background);
            Thread.Sleep(100);
        }
    }

Here is my solution for simulating the passage of cards one by one. The problem was in threading!

Eugen
  • 19
  • 5