WPF XAML-具有可变索引的绑定数组

WPF XAML-具有可变索引的绑定数组,wpf,xaml,Wpf,Xaml,我想使用数组进行数据绑定: <Button x:Name="ButtonMap" Content="{Binding Tag[0], RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"> 现在,我将使用这个索引来获取数组的第n个元素。就像这样: {Binding Path=(ItemsControl.AlternationIndex), RelativeSource={

我想使用数组进行数据绑定:

<Button x:Name="ButtonMap" Content="{Binding Tag[0], RelativeSource={RelativeSource  AncestorType={x:Type ItemsControl}}}">
现在,我将使用这个索引来获取数组的第n个元素。就像这样:

{Binding Path=(ItemsControl.AlternationIndex), 
            RelativeSource={RelativeSource TemplatedParent}, 
            StringFormat={}Index is {0}}
<Button x:Name="ButtonMap" Content="{Binding Tag[index], RelativeSource={RelativeSource  AncestorType={x:Type ItemsControl}}}">

我试图使用StaticResource,但绑定不起作用

你能帮我吗

编辑: XAML文件:

`<UserControl x:Class="INSAWorldWPF.Views.GameView"
         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"
         xmlns:custom="clr-namespace:INSAWorldWPF"
         xmlns:local="clr-namespace:INSAWorldWPF.ViewModels"
         xmlns:custom1="clr-namespace:INSAWorld;assembly=INSAWorld"
         xmlns:System="clr-namespace:System;assembly=mscorlib"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Background>
    <ImageBrush ImageSource="/Views/Resources/background2.jpg"/>
</UserControl.Background>

<!--<Button Height="50" Margin="0,30" Command="{Binding TestCommand, Mode=OneWay}" Content="Demo Map"/>-->
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="2*"/>
        </Grid.ColumnDefinitions>

        <StackPanel Grid.Column="0" VerticalAlignment="Center" Margin="30,0,0,0">
            <StackPanel.Background>
                <SolidColorBrush Color="Gray" Opacity=".3"/>
            </StackPanel.Background>
            <Image Source="{Binding ImageRace}" Width="200" Height="200" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,20" />
            <TextBlock Text="{Binding PseudoCurrentPlayer}" FontFamily="pack://application:,,,/Views/Resources/#Ringbearer Medium" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="60"/>
            <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
                <TextBlock HorizontalAlignment="Center" FontFamily="pack://application:,,,/Views/Resources/#Cosmic Love" VerticalAlignment="Center" FontSize="40" Text="LifePoint : "/>
                <TextBlock HorizontalAlignment="Center" FontFamily="pack://application:,,,/Views/Resources/#Cosmic Love" VerticalAlignment="Center" FontSize="40" Text="{Binding LifePoint}"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
                <TextBlock HorizontalAlignment="Center" FontFamily="pack://application:,,,/Views/Resources/#Cosmic Love" VerticalAlignment="Center" FontSize="40" Text="MovePoint : "/>
                <TextBlock HorizontalAlignment="Center" FontFamily="pack://application:,,,/Views/Resources/#Cosmic Love" VerticalAlignment="Center" FontSize="40" Text="{Binding MovePoint}"/>
            </StackPanel>
        </StackPanel>

            <StackPanel Grid.Column="1" VerticalAlignment="Center" Margin="30,0">
            <StackPanel.Background>
                <SolidColorBrush Color="Gray" Opacity=".3"/>
            </StackPanel.Background>

        <ItemsControl ItemsSource="{Binding ListTile}" AlternationCount="150" Tag="{Binding UnitByTile}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <DataTemplate.Resources>
                        <custom:TypeOfConverter x:Key="typeOfConverter" />
                        <System:Double x:Key="theMargin">0</System:Double>
                    </DataTemplate.Resources>
                    <Button x:Name="ButtonMap" Content="{Binding Path=Tag[?????????], RelativeSource={RelativeSource  AncestorType={x:Type ItemsControl}}}">

                        <Button.Resources>

                        </Button.Resources>
                        <Button.Style>
                            <Style TargetType="Button">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type Button}">
                                            <Border Background="{TemplateBinding Background}" BorderBrush="Black" BorderThickness="1">
                                                <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                            </Border>
                                            <ControlTemplate.Triggers>
                                                <Trigger Property="IsMouseOver" Value="True">
                                                    <Setter Property="Opacity" Value="1" />
                                                </Trigger>
                                                <Trigger Property="IsMouseOver" Value="False">
                                                    <Setter Property="Opacity" Value="0.7" />
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ., Converter={StaticResource typeOfConverter} }" Value="Desert">
                                        <Setter Property="Background" Value="Yellow" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding ., Converter={StaticResource typeOfConverter} }" Value="Swamp">
                                        <Setter Property="Background" Value="DarkKhaki" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding ., Converter={StaticResource typeOfConverter} }" Value="Volcano">
                                        <Setter Property="Background" Value="Red" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding ., Converter={StaticResource typeOfConverter} }" Value="Plain">
                                        <Setter Property="Background" Value="ForestGreen" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </Button.Style>
                    </Button>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid Columns="{Binding SizeMap}" Rows="{Binding SizeMap}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Height="600"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </StackPanel>
    </Grid>
`
0

`

我建议使用
多值转换器。假设
Tag[]
string[]
类型,则可以使用以下命令:

public class ArrayItemSelector : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        //Check given arguments first
        if(!(values.Length > 1) || !(values[0] is string[]) || !(values[1] is int))
            throw new ArgumentException("given values not correct");

        return ((string[]) values[0])[(int) values[1]];
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
用法:

<Grid.Resources>
    <namespace:ArrayItemSelector x:Key="ArrayItemSelector" />
</Grid.Resources>
<!-- .................. -->
<Button x:Name="ButtonMap">
    <Button.Content>
         <MultiBinding Converter="{StaticResource ArrayItemSelector}">
             <Binding Path="Tag" RelativeSource="{RelativeSource  AncestorType={x:Type ItemsControl}}" />
             <Binding Path="(ItemsControl.AlternationIndex)" RelativeSource="{RelativeSource TemplatedParent}" />
         </MultiBinding>
    </Button.Content>
</Button>

简单的
ValueConverter
无法工作,因为
ConverterParameter
需要是静态的。您要使用的索引需要绑定


希望这对您有用

标记数组是否与ItemsControl绑定到的集合相同?你能发布itemscontrol的XAML吗?我用XAML编辑了我的问题