2016-12-29 115 views
0

我愿做一个绑定一个数组:WPF XAML - 绑定数组可变指数

<Button x:Name="ButtonMap" Content="{Binding Tag[0], RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"> 

它的工作原理。

但是,我的按钮是在ItemsControl中,我成功地获得当前项目的索引这样的:

{Binding Path=(ItemsControl.AlternationIndex), 
      RelativeSource={RelativeSource TemplatedParent}, 
      StringFormat={}Index is {0}} 

现在,我会用这个指数来获得我的数组的第n个元素。像这样:

<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

标签数组是否与您的ItemsControl绑定的集合相同?你可以发布itemscontrol的XAML吗? – plast1k

+0

我使用XAML编辑了我的问题 – captainhaddock

回答

0

我会建议MultiValueConverter的使用。假设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需求是静态的。您想使用的索引需要被绑定。

希望这适用于您