Здравствуйте, столкнулся с проблемой что проверенный алгоритм нумерации строк в ListBox не корректно работает с ComboBox. А именно при активации выпадающего списка строки пронумерованны правильно, но у выбранного элемента нумерация строки не отображается в ComboBox.
ВАРИАНТ 1 через конвертер
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBoxItem}}, Converter={StaticResource RowNumberConverter}, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Margin="1" Text="{Binding}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
MessageBox.Show("RowNumberConverter");
if (value is ListViewItem)
{
var item = (ListViewItem)value;
var listView = ItemsControl.ItemsControlFromItemContainer(item) as ListView;
int index = listView.ItemContainerGenerator.IndexFromContainer(item);
return index.ToString(CultureInfo.InvariantCulture);
}
if (value is ComboBoxItem)
{
var item = (ComboBoxItem)value;
var comboBox = ItemsControl.ItemsControlFromItemContainer(item) as ComboBox;
int index = comboBox.ItemContainerGenerator.IndexFromContainer(item);
return index.ToString(CultureInfo.InvariantCulture);
}
return null;
}
вариант 2 через AlternationCount
<ComboBox Grid.Row="1"
MinWidth="100"
HorizontalAlignment="Center"
VerticalAlignment="Center"
AlternationCount="{Binding Path=Signals.Count,
UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Path=Signals}"
SelectedItem="{Binding Path=SelectedIndex,
UpdateSourceTrigger=PropertyChanged}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplatedParent.(ItemsControl.AlternationIndex), UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Margin="1" Text="{Binding}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Не 1 не 2 вариант не работают. В отладчике посмотрел ход выполнения Конвертера, и он выполняется только при отрисовке списка при первом обращении, нумерует строки, при принятии какого либо значения конвертер не срабатывает, т.к. не срабатывает Binding (не меняется ComboBoxItem). Прошу помочь как отображать в выбранном элементе ComboBox номер строки.