将组合框放入网格中会使所选项目为空

时间:2017-09-04 23:54:44

标签: c# windows xaml combobox uwp

我有两个组合框,我将彼此展示。 我正在使用一个网格和两列用于此...但是当我这样做时,初始选择的comboxbox项目消失了

所以,如果我把它们放在网格中...我得到这个: enter image description here

如果我删除网格......组合框获得初始值... xaml看起来像这样......这里有网格部分注释掉了......我只是不明白为什么添加/删除网格会产生影响......

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <StackPanel>
        <TextBlock Text="{x:Bind ViewModel.LoadErrorMessage, Mode=OneWay}" />

        <!--<Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="5*"/>
            </Grid.ColumnDefinitions>-->
            <ComboBox HorizontalAlignment="Stretch" ItemsSource="{x:Bind ViewModel.WeaponCountRange}" SelectedItem="{x:Bind ViewModel.WeaponCount, Mode=TwoWay}"></ComboBox>
            <ComboBox Grid.Column="1" HorizontalAlignment="Stretch" ItemsSource="{x:Bind ViewModel.Weapons}" SelectedItem="{x:Bind ViewModel.SelectedWeapon, Mode=TwoWay}" >
                <ComboBox.ItemTemplate>
                    <DataTemplate>
                        <Border Background="Black">
                            <StackPanel Orientation="Horizontal">
                                <Image Source="{Binding ImageFile}" Stretch="Uniform" Height="48"></Image>
                                <TextBlock Foreground="Yellow" Height="48" VerticalAlignment="Stretch" Text="{Binding Name}"></TextBlock>
                                <Image VerticalAlignment="Top" Visibility="{Binding ShieldPiercingVis}" Height="12" Source="/Assets/ship_modules/dragon_missile.png"/>
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </ComboBox.ItemTemplate>
            </ComboBox>
        <!--</Grid>-->
        <Border>

enter image description here

填充后面的代码是异步任务...请参阅以下内容

public ObservableCollection<WeaponViewModel> Weapons = new ObservableCollection<WeaponViewModel>();
private WeaponViewModel _selectedWeapon;
public WeaponViewModel SelectedWeapon
{
    get => _selectedWeapon;
    set => SetProperty(ref _selectedWeapon, value);
}
private async Task Initialize()
    {
        {
            var wRepo = new WeaponRepository();
            await wRepo.Initialize();
            foreach (var item in wRepo.Weapons)
            {
                Weapons.Add(new WeaponViewModel(item));
                if (Weapons.Count == 1)
                    SelectedWeapon = Weapons[0];
            }
        }
    ...

1 个答案:

答案 0 :(得分:0)

我无法重现您的问题。当我运行你的代码时,它会抛出异常。我发现您没有为IValueConverter设置SelectedItem。我为SelectedItem创建了转换器,它在我身边运行良好。我将上传您可以参考的code sample

<强>转换器

public class Converter : IValueConverter
 {
     public object Convert(object value, Type targetType, object parameter, string language)
     {
         return value;

     }

     public object ConvertBack(object value, Type targetType, object parameter, string language)
     {
         return value as ComboBoxItem;
     }
 }

enter image description here