Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF-如何将图像添加到uniformgrid按钮?_C#_Wpf_Image_Xaml_Uniformgrid - Fatal编程技术网

C# WPF-如何将图像添加到uniformgrid按钮?

C# WPF-如何将图像添加到uniformgrid按钮?,c#,wpf,image,xaml,uniformgrid,C#,Wpf,Image,Xaml,Uniformgrid,我有两个图像定义如下: Image Wall = new Image(); Wall.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/Wall.png")); Image Corridor = new Image(); Corridor.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/Corridor.png")); 我有一

我有两个图像定义如下:

Image Wall = new Image();
Wall.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/Wall.png"));
Image Corridor = new Image();
Corridor.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/Corridor.png"));
我有一个统一的网格,每个单元格中都有一个按钮。我的目标是根据DataTrigger条件使这些图片中的一个出现在每个按钮上。我的代码正在运行,但图片从未出现,我收到两条警告:“资源“走廊”无法解析”和“资源”墙“无法解析”。以下是我的xaml代码:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <ItemsControl Grid.Row="1" ItemsSource="{Binding Fields}">

        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid Rows="{Binding TableSize}" Columns="{Binding TableSize}" />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Button Command="{Binding SelectCommand}" CommandParameter="{Binding Number}" BorderThickness="0">

                    <Button.RenderTransform>
                        <ScaleTransform ScaleX="1" ScaleY="1" />
                    </Button.RenderTransform>
                    <Button.Style>
                        <Style TargetType="Button">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Background}" Value="Corridor">
                                    <Setter Property="Foreground" Value="{DynamicResource Corridor}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Background}" Value="Wall">
                                    <Setter Property="Foreground" Value="{DynamicResource Wall}" />
                                </DataTrigger>
                                <DataTrigger Binding="{Binding Background}" Value="Player">
                                    <Setter Property="Background" Value="Yellow" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                </Button>
            </DataTemplate>
        </ItemsControl.ItemTemplate>

        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Grid.Row" Value="{Binding X}" />
                <Setter Property="Grid.Column" Value="{Binding Y}" />
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>
</Grid>

最后,这是我绑定buttongrid的可见集合。它的background属性从一个名为_model的值中获取其值,因为我使用的是MVVM体系结构,这与本问题无关。重要的是背景属性只能是走廊、墙或播放器

ObservableCollection<DungeonField> Fields = new ObservableCollection<DungeonField>();
for (Int32 i = 0; i < 9; ++i)
{
    for (Int32 j = 0; j < 9; ++j)
        Fields.Add(new DungeonField
        {
            Background = _model.GetField(i, j).ToString(),
            X = i,
            Y = j,
            Number = i * 9 + j,
        });
}
ObservableCollection字段=新的ObservableCollection();
对于(Int32 i=0;i<9;++i)
{
对于(Int32 j=0;j<9;++j)
字段。添加(新地下城字段)
{
Background=\u model.GetField(i,j).ToString(),
X=i,
Y=j,
编号=i*9+j,
});
}
我将这两个图像作为资源添加到我的项目中,并且它们的构建操作设置为Resource。我是一个在wpf中处理图片的新手,所以我的问题是我遗漏了什么

提前谢谢

编辑:
我想我不应该在DataTrigger部分的xaml中使用DynamicSource,因为我已经声明了一个Image变量,所以我可以将它绑定到按钮。使用绑定而不是DynamicSource会使所有这些警告消失,但图片仍然不会显示在网格中。

我知道了。我不需要在问题开始时声明这两个图像,前两个DataTrigger应该如下所示:

<DataTrigger Binding="{Binding Background}" Value="Corridor">
    <Setter Property="Source" Value="Resources/Corridor.png" />
</DataTrigger>
<DataTrigger Binding="{Binding Background}" Value="Wall">
    <Setter Property="Source" Value="Resources/Wall.png" />
</DataTrigger>