C# 获取IitemControl中的项,IitemControl是另一个ItemControl
我有个问题我有一个简单的WPF项目列表:C# 获取IitemControl中的项,IitemControl是另一个ItemControl,c#,wpf,xaml,C#,Wpf,Xaml,我有个问题我有一个简单的WPF项目列表: <ItemsControl ItemsSource="{Binding Details.ModuleFunStateInRack}" x:Name="ModuleFunStateInRackRoot"> <ItemsControl.ItemTemplate> <DataTemplate> <StackPanel Orientation
<ItemsControl ItemsSource="{Binding Details.ModuleFunStateInRack}" x:Name="ModuleFunStateInRackRoot">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<ItemsControl ItemsSource="{Binding ModuleFunState}" x:Name="ModuleFunStateRoot">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Label Content="Module Functional State Id: "/>
<TextBlock VerticalAlignment="Center" >
<TextBlock.Text>
<Binding>
<Binding.Converter>
<helpers:ItemIndexConverter DataContext="{Binding Source={x:Reference ModuleFunStateRoot},Path=(ItemsControl.ItemsSource)}" />
</Binding.Converter>
</Binding>
</TextBlock.Text>
</TextBlock>
</StackPanel>>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
问题是,当我在内部ItemControl上使用此帮助程序时,我发现他找不到名为ModuleFunStateRoot name的元素。是否有一个我复制并传递的元素,以确保引用标记,但当我在OutermorModuleFunStateInTrackRoot ItemControl上使用此帮助程序时,它工作正常。可能有什么问题?您可以使用和IMultiValueConverter并绑定到ItemsSource和当前项:
public sealed class ItemIndexConverter : IMultiValueConverter
{
public object Convert(object[] dataItems, Type targetType, object props, CultureInfo _) =>
((IList)dataItems[1]).IndexOf(dataItems[0]).ToString();
public object[] ConvertBack(object value, Type[] targetTypes, object props, CultureInfo _) =>
throw new NotImplementedException();
}
XAML:
能否显示用于在内部ItemsControl上使用助手的代码行?请注意,DataTemplate有自己的名称作用域,这使得ModuleFunStateRoot名称在其作用域之外未定义。
public sealed class ItemIndexConverter : IMultiValueConverter
{
public object Convert(object[] dataItems, Type targetType, object props, CultureInfo _) =>
((IList)dataItems[1]).IndexOf(dataItems[0]).ToString();
public object[] ConvertBack(object value, Type[] targetTypes, object props, CultureInfo _) =>
throw new NotImplementedException();
}
<TextBlock VerticalAlignment="Center" >
<TextBlock.Text>
<MultiBinding>
<MultiBinding.Converter>
<helpers:ItemIndexConverter />
</MultiBinding.Converter>
<Binding />
<Binding Path="ItemsSource" RelativeSource="{RelativeSource AncestorType=ItemsControl}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>