【问题标题】:WPF XAML - Binding Array with variable indexWPF XAML - 具有变量索引的绑定数组
【发布时间】:2016-12-29 19:28:35
【问题描述】:

我想用一个数组进行数据绑定:

<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>

`

【问题讨论】:

  • 标签数组是否与您的 ItemsControl 绑定的集合相同?您可以发布项目控件的 XAML 吗?
  • 我用 XAML 编辑了我的问题

标签: wpf xaml


【解决方案1】:

我建议使用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 需要是静态的。您要使用的索引需要绑定。

希望这对你有用

【讨论】:

    猜你喜欢
    • 2015-09-15
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 2015-11-23
    • 2011-07-15
    • 1970-01-01
    • 2013-04-13
    相关资源
    最近更新 更多