Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.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# 在网格的一行中绑定多个元素_C#_Xaml_Binding_Rows - Fatal编程技术网

C# 在网格的一行中绑定多个元素

C# 在网格的一行中绑定多个元素,c#,xaml,binding,rows,C#,Xaml,Binding,Rows,我想有5列并绑定元素列表。每个元素都有一张图片。我想在一行中显示5个元素的图片 实际上这是我的代码 <ListBox ItemsSource="{Binding Buildings}" Width="350" Margin="0,5,0,10"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" CanHor

我想有5列并绑定元素列表。每个元素都有一张图片。我想在一行中显示5个元素的图片

实际上这是我的代码

<ListBox ItemsSource="{Binding Buildings}" Width="350" Margin="0,5,0,10">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" CanHorizontallyScroll="True">
                <!--<TextBlock Padding="5,0,5,0" Text="{Binding Name}" />-->
                <Image Source="{Binding Name,Converter={StaticResource CivToImage}}" Height="50px" Width="50px"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我还想用工具提示提供诸如名称(Buildings.Name)和成本(Buildings.Costs)之类的信息

不幸的是,我一行只能显示一个元素。。。 我尝试了一个网格解决方案,但它每行只显示一个图像:

<DataGrid ItemsSource="{Binding Buildings}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Name" Width="SizeToCells" IsReadOnly="True">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Image Source="{Binding Name,Converter={StaticResource CivToImage}}" Height="50px" Width="50px"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>


感谢您的帮助。

当您使用网格时,如:

<Grid>
    <Image Name ="Image1" Source="{Binding Name,Converter={StaticResource CivToImage}}"  Height="50px" Width="50px"/>
    <Image Name ="Image2" Source="{Binding Name,Converter={StaticResource CivToImage}}" Height="50px" Width="50px"/>
</Grid>

所有项目都将定位在0,0,因此您将只看到一个位于顶部。你们可以像第一个是0,0秒,在这个例子中是50px,0

但我认为最好的办法是加上:

<StackPanel Orientation="Horizontal">
    <Image Name ="Image1" Source="{Binding Name,Converter={StaticResource CivToImage}}" Height="50px" Width="50px"/>
    <Image Name ="Image2" Source="{Binding Name,Converter={StaticResource CivToImage}}" Height="50px" Width="50px"/>
 </StackPanel>


这将使您的所有项目彼此相邻。希望是这样,因为我不是100%确定我理解了这个问题。

为什么它不起作用,它做了什么/说了什么?它只显示了一列…我能看到它失败的唯一原因是因为你在单个建筑层绑定不起作用。sry当我说它不起作用时,我的意思是它不是我想要的,我想用数据显示5列,但每列显示一个图像。不幸的是,这不起作用,我有2个项目,但它们是相同的项目。。。我想得到同一行中的下一项好的,那么你能解释一下你的建筑类是什么样子的吗?CivToImageConverter到底做什么?