我想用数组进行数据绑定:
<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,但Binding不起作用。
你能帮帮我吗?编辑: 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 :(得分: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
需要是静态的。您想要使用的索引需要绑定。
希望这适合你