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を使用しようとしましたが、バインディングが機能しませんでした。
お願いします。
EDIT: 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がバインドされているのと同じコレクションですか? itemscontrolのXAMLを投稿できますか? – plast1k
私の質問をXAMLで編集しました – captainhaddock