Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get hexes to show up using MVVM. #5

Open
BeyondBelief96 opened this issue Jan 15, 2023 · 1 comment
Open

Can't get hexes to show up using MVVM. #5

BeyondBelief96 opened this issue Jan 15, 2023 · 1 comment

Comments

@BeyondBelief96
Copy link

BeyondBelief96 commented Jan 15, 2023

Hello, I'm attempting to use this project to make a hex based board game. I'm not sure if you're still active or not, but if you have the time to answer my question it would be greatly appreciated.

I currently have a custom control called MapGridView.xaml and is as shown below:

<Grid>
    <hx:HexList Grid.Row="0" Grid.Column="0" Name="Board" 
            Margin="5"
            RowCount="6" ColumnCount="8"
            Orientation="Vertical" ItemsSource="{Binding Tiles}">
        <hx:HexList.ItemContainerStyle>
            <Style TargetType="{x:Type hx:HexItem}">
                <Setter Property="Grid.Row" Value="{Binding Path=X}"/>
                <Setter Property="Grid.Column" Value="{Binding Path=Y}"/>
                <Setter Property="BorderThickness" Value="1.5"/>
                <Setter Property="FontSize" Value="16"/>
                <Setter Property="Background" Value="White"/>
            </Style>
        </hx:HexList.ItemContainerStyle>
    </hx:HexList>
</Grid>

Which has a datacontext of the following viewmodel:

`public class MapGridViewModel : ViewModelBase
{
#region Fields

    private ObservableCollection<Coordinate2D> _tileCoordinates;

    #endregion

    #region Ctor

    public MapGridViewModel(HexGrid hexGrid)
    {
        Tiles = new ObservableCollection<Coordinate2D>(hexGrid.Hexes);
    }

    #endregion

    #region Properties

    public ObservableCollection<Coordinate2D> Tiles
    {
        get { return _tileCoordinates; }
        set
        {
            if (_tileCoordinates == value) return;
            _tileCoordinates = value;
            OnPropertyChanged(nameof(Tiles));
        }
    }

    #endregion
}`

When I run the application all I get is a single hex on the screen. I've checked to ensure that my Tiles are indeed populated with many coordinates, so I'm not sure why each row/column in the control is not populating. I checked your demo to see how you did your board and saw you setup the itemssource in the codebehind and just bound it too a list which was just a struct you made, similar to my Coordinate2D object. I tried that and it worked fine. So I'm not sure why the bindings to the X and Y property on my Coordinate2D object is not working.

I couldn't get some of the code to format properly in github, sorry about that.

@BeyondBelief96
Copy link
Author

I tried instead using your IntPoint data type for my ObservableCollection type instead of ObservableCollection which comes from another nuget package called HexCore. The bindings work fine using IntPoint, but not using Coordinate2D. I'm not sure what is the issue. Did your code specifically make it so that the bindings work with only that IntPoint type from your demo code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant