ComboBox缺少项目

时间:2018-01-08 11:06:04

标签: c# wpf xaml mvvm combobox

我遇到ComboBox的奇怪问题。我想在ComboBox中表示组合键,其中每个键都在Canvas上。

Binding工作正常,初始化后看起来没问题,但当我点击它并展开下拉列表时,当前所选项目将为空,如果我在一段时间后重复更改所选项目下拉列表物品也将是空的。

当然,初始化后Keys集合不会改变。我使用自定义样式,但也使用默认样式。

以下是关于所述行为的简短gif:

Example

我使用MVVM模式(MVVM Light),我的VM如下:

public class SettingsViewModel : ViewModelBase
{
        private ObservableCollection<List<VirtualKeyCode>> _Keys;
        public ObservableCollection<List<VirtualKeyCode>> Keys
        {
            get => _Keys;
            set => Set(() => Keys, ref _Keys, value);
        }
}

XAML片段:

<ComboBox MinHeight="42" Margin="5,0" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding Keys, Mode=OneWay}" SelectedIndex="0">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Margin="5,0" Orientation="Horizontal" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Border>
                            <Viewbox Width="Auto" Height="40" Margin="5,0" HorizontalAlignment="Left" VerticalAlignment="Center" Stretch="Uniform">
                                <ContentControl HorizontalAlignment="Left" VerticalAlignment="Center" Content="{Binding Converter={Converters:StringToResourceConverter}}" />
                            </Viewbox>
                        </Border>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

在其他情况下使用的转换器正常工作:

public class StringToResourceConverter : MarkupExtension, IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value != null ? Application.Current.TryFindResource(value.ToString()) : null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return Binding.DoNothing;
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

键在Canvas上定义为资源,例如T键为:

<Canvas x:Key="VK_T" Width="53" Height="53.531">
    <Canvas.Resources>
        <LinearGradientBrush x:Key="linearGradient3646" MappingMode="Absolute" StartPoint="-80,65" EndPoint="-4,65">
            <LinearGradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0" Color="#D8D8D8" />
                    <GradientStop Offset="1" Color="#B1B1B1" />
                </GradientStopCollection>
            </LinearGradientBrush.GradientStops>
            <LinearGradientBrush.Transform>
                <MatrixTransform Matrix="0.2087115 0 0 0.2080024 309.2826 408.16259" />
            </LinearGradientBrush.Transform>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="linearGradient3650" MappingMode="Absolute" StartPoint="333,520" EndPoint="356,536">
            <LinearGradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0" Color="#656565" />
                    <GradientStop Offset="1" Color="#939393" />
                </GradientStopCollection>
            </LinearGradientBrush.GradientStops>
            <LinearGradientBrush.Transform>
                <MatrixTransform Matrix="0.2087115 0 0 0.2080024 282.55261 440.92403" />
            </LinearGradientBrush.Transform>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="linearGradient3653" MappingMode="Absolute" StartPoint="339,522" EndPoint="359,538">
            <LinearGradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0" Color="#D6D6D6" />
                    <GradientStop Offset="1" Color="#939393" />
                </GradientStopCollection>
            </LinearGradientBrush.GradientStops>
            <LinearGradientBrush.Transform>
                <MatrixTransform Matrix="-0.2087115 0 0 0.2080024 467.43777 440.92403" />
            </LinearGradientBrush.Transform>
        </LinearGradientBrush>
        <LinearGradientBrush x:Key="linearGradient3657" MappingMode="Absolute" StartPoint="529,334" EndPoint="526,321">
            <LinearGradientBrush.GradientStops>
                <GradientStopCollection>
                    <GradientStop Offset="0" Color="#D6D6D6" />
                    <GradientStop Offset="1" Color="#656565" />
                </GradientStopCollection>
            </LinearGradientBrush.GradientStops>
            <LinearGradientBrush.Transform>
                <MatrixTransform Matrix="0.2087115 0 0 0.2080024 282.55261 440.92403" />
            </LinearGradientBrush.Transform>
        </LinearGradientBrush>
    </Canvas.Resources>
    <Canvas>
        <Canvas.RenderTransform>
            <TranslateTransform X="-348.5" Y="-505.59375" />
        </Canvas.RenderTransform>
        <Rectangle Canvas.Left="349.25" Canvas.Top="506.34" Width="51.494" Height="52.044" Fill="#FF656565" RadiusX="4.7705" RadiusY="4.0115" />
        <Rectangle Canvas.Left="389.44" Canvas.Top="510.2" Width="11.279" Height="34.564" Fill="#FFD6D6D6" />
        <Path Data="m391.6 513.22 9.0939-2.9715c-0.3613-2.8568-2.3291-3.8457-5.0687-3.9372-3.6454 0.10572-7.2028 0.26014-9.0939 1.3372l5.0687 5.5715z" Fill="{StaticResource linearGradient3657}" />
        <Rectangle Canvas.Left="358.35" Canvas.Top="545.4" Width="33.469" Height="12.975" Fill="#FF929292" RadiusX="0" RadiusY="0" />
        <Path Data="m389.9 542.35 10.805 1.1556 0.0395 10.506c-0.006 2.7094-2.1668 4.2661-4.4011 4.3599l-4.8096-0.0525-5.3234-12.344 3.6895-3.6245z" Fill="{StaticResource linearGradient3653}" />
        <Path Data="m356.76 542.25-7.4845 1.2607-0.0395 10.506c0.006 2.7094 2.1668 4.2661 4.4011 4.3599l4.8096-0.0525 1.6866-12.292-3.3733-3.782z" Fill="{StaticResource linearGradient3650}" />
        <Rectangle Canvas.Left="356.02" Canvas.Top="507" Width="36.535" Height="40.245" Fill="#FFF1F1F1" RadiusX="4.7705" RadiusY="4.7543" />
        <Rectangle Canvas.Left="356.02" Canvas.Top="507" Width="35.481" Height="40.245" Fill="{StaticResource linearGradient3646}" RadiusX="4.7705" RadiusY="4.7543" />
        <TextBlock Canvas.Left="360.15726" Canvas.Top="511.7" FontFamily="Arial" FontSize="14" FontStyle="normal" FontWeight="Normal" Foreground="#FF000000">
            <TextBlock xml:space="preserve" FontSize="14">T</TextBlock>
        </TextBlock>
        <Rectangle Canvas.Left="349.25" Canvas.Top="506.34" Width="51.494" Height="52.044" RadiusX="4.7706" RadiusY="4.0115" Stroke="#FF000000" StrokeEndLineCap="Flat" StrokeLineJoin="Miter" StrokeMiterLimit="4" StrokeStartLineCap="Flat" StrokeThickness="1.5" />
    </Canvas>
</Canvas>

问候,大卫

1 个答案:

答案 0 :(得分:0)

尝试将x:Shared资源上的Canvas属性设置为False

<Canvas x:Key="VK_T" x:Shared="False" ...>