获取 ItemsControl MVVM 中的当前项目

Get the current item in ItemsControl MVVM

我用字典中的一些键列表填充 ItemsControl,我建议用户映射每个键,打印在 Label 中,一些值在 ComboBox 中就在下面。

最后,我必须得到 ComboBox 中所选值对应的 Label 键,以便填充字典值。

我的所有控件都以 MVVM 模式绑定到 ViewModel 属性。

<ItemsControl x:Name="icMapping"
          HorizontalAlignment="Center" MaxHeight="120"
          ItemsSource="{Binding ColumnsMapping}">

<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel/>
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>

<ItemsControl.Template>
    <ControlTemplate>
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <ItemsPresenter/>
        </ScrollViewer>
    </ControlTemplate>
</ItemsControl.Template>

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <StackPanel HorizontalAlignment="Center" Margin="6">
            <Label x:Name="lblPropertyKey"
                   Content="{Binding ApiPropertyKey}"
                   Width="250"/>
            <ComboBox x:Name="cboxCsvHeaders"
                      Width="250"
                      ItemsSource="{Binding
                                    RelativeSource={RelativeSource Mode=FindAncestor,
                                                                    AncestorType=Window},
                                    Path=DataContext.CsvTemplateFile.CsvHeaders}"
                      SelectedItem="{Binding
                                    RelativeSource={RelativeSource Mode=FindAncestor,
                                                                    AncestorType=Window},
                                    Path=DataContext.SelectedCsvHeader, Mode=OneWayToSource}"/>
        </StackPanel>
    </DataTemplate>
</ItemsControl.ItemTemplate>

我试图遍历 ItemsControl 中的 ItemsTemplate,但我 ComboBox。在绑定 SelectedItem 中的值后填充文本。

private string selectedCsvHeader;
public string SelectedCsvHeader
{
    get => selectedCsvHeader;
    set
    {
        selectedCsvHeader = value;
        if (value != null)
        {
            var ic = this.configView.icMapping;
            for (int i = 0; i < ic.Items.Count; i++)
            {
                System.Windows.Controls.ContentPresenter cp = (System.Windows.Controls.ContentPresenter)ic.ItemContainerGenerator.ContainerFromItem(ic.Items[i]);
                System.Windows.Controls.ComboBox cbox = cp.ContentTemplate.FindName("cboxCsvHeaders", cp) as System.Windows.Controls.ComboBox;
                System.Windows.Controls.Label lbl = cp.ContentTemplate.FindName("lblPropertyKey", cp) as System.Windows.Controls.Label;

                MessageBox.Show((cbox.Text == selectedCsvHeader).ToString()); // False
            }
            FillDgPreview();
        }
        OnPropertyChanged(nameof(SelectedCsvHeader));
    }
}

我使用 ItemsControl 因为我事先不知道我的字典中有多少键。 (在网上搜索后) Here an image to explain

提前致谢!

为了设置字典条目的值,您必须使用允许更改其 [=24= 的 Value 属性 的字典 class ] 对条目。

因为 Dictionary<TKey,TValue> 不允许这样做(因为 KeyValuePair<TKey,TValue> 是不可变的),你可以编写自己的字典 class,可能比这个例子更复杂:

public class KeyValue
{
    public KeyValue(string key, string value = null)
    {
        Key = key;
        Value = value;
    }

    public string Key { get; }
    public string Value { get; set; }
}

public class ViewModel
{
    public List<KeyValue> Map { get; } = new List<KeyValue>();

    public List<string> Keys { get; } = new List<string>();
}

您现在将绑定该视图模型,如下所示 XAML,其中 {Binding Key}{Binding Value} 使用字典条目(即 Map 集合元素)作为源对象。如果您使用具有不同条目类型的自定义字典 ​​class,这些绑定看起来会有所不同。

<ItemsControl ItemsSource="{Binding Map}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Key}"/>
                <ComboBox SelectedItem="{Binding Value}"
                          ItemsSource="{Binding DataContext.Keys,
                                        RelativeSource={RelativeSource AncestorType=ItemsControl}}"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>