1

When I swap my grid control for a stackpanel control the contents inside disappear. I could just use the grid control but not knowing bugs me :)

this works

<Grid> <views:ChessBoardView DataContext="{Binding Path=BoardViewModel}" ></views:ChessBoardView>
</Grid>

This doesn't:

<StackPanel>
<views:ChessBoardView DataContext="{Binding Path=BoardViewModel}" ></views:ChessBoardView>
</StackPanel>

Details:

View Model:

public class MainWindowViewModel : BaseViewModel
{
    #region Properties

    #region ChessBoardViewModel

    private ChessBoardViewModel _BoardViewModel;
    public ChessBoardViewModel BoardViewModel
    {
        get { return _BoardViewModel; }
        set
        {
            if (_BoardViewModel != value)
            {
                _BoardViewModel = value;
                RaisePropertyChanged(() => BoardViewModel);
            }
        }
    }

    #endregion
}

namespace Chess_Piece_Viewer.ViewModels
{
public class ChessBoardViewModel:BaseViewModel
{
    public Color DarkSquareColor { get; set; }
    public Color LightSquareColor { get; set; }
    public ChessBoardViewModel()
    {
        DarkSquareColor = Colors.Black;
        LightSquareColor = Colors.White;
    }

}

Poor Mans Dependency Injection:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);
        MainWindow = new Chess_Piece_Viewer.Views.MainWindow();
        var MainViewModel = new Chess_Piece_Viewer.ViewModels.MainWindowViewModel();
        MainViewModel.BoardViewModel = new ViewModels.ChessBoardViewModel();
        MainWindow.DataContext = MainViewModel;
        MainWindow.Show();

    }
}

I've added the usercontrol that is disappearing

<UserControl x:Class="Chess_Piece_Viewer.Views.ChessBoardView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>

</UserControl.Resources>
<UniformGrid Rows="8" Columns="8">
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
       </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=DarkSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>
    <Rectangle Stroke="Black">
        <Rectangle.Fill>
            <SolidColorBrush Color="{Binding Path=LightSquareColor}"/>
        </Rectangle.Fill>
    </Rectangle>



</UniformGrid>
</UserControl>

Thanks in advance for any help :)

4

1 回答 1

0

我会推荐两件事:

首先,在代码隐藏中使用一些循环来生成所有矩形。您有大约 64 个硬编码。后面的代码确实可以减小 XAML 文件的大小。

XAML:

 <UniformGrid x:Name="_uniformGrid" Rows="8" Columns="8"/>

CodeBehind:(有点复杂,因为行改变了白色/黑色,但下面的代码有效......)

private void InitializeUniformGrid()
{
    for (int i = 0; i < 4; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            _uniformGrid.Children.Add(new Rectangle { Fill = Brushes.Black, Stroke = Brushes.Black });
            _uniformGrid.Children.Add(new Rectangle { Fill = Brushes.White, Stroke = Brushes.Black });
        }
        for (int k = 0; k < 4; k++)
        {
            _uniformGrid.Children.Add(new Rectangle { Fill = Brushes.White, Stroke = Brushes.Black });
            _uniformGrid.Children.Add(new Rectangle { Fill = Brushes.Black, Stroke = Brushes.Black });
        }
    }
}

其次,要解决您的主要问题,您需要为 ChessBoardView 指定高度和宽度。

这样做(将 500 更改为您真正需要的任何宽度和高度):

Height="500" Width="500"

以下只会在设计视图中设置高度和宽度,而不是在代码运行时设置:

d:DesignHeight="300" d:DesignWidth="300"

生成的 XAML 应如下所示:

<UserControl x:Class="Chess_Piece_Viewer.Views.ChessBoardView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         Height="300" Width="300"
         d:DesignHeight="300" d:DesignWidth="300">
于 2013-07-22T14:52:33.200 回答