在Combobox中取消选择数据

时间:2018-03-14 10:36:01

标签: c# uwp uwp-xaml

我有一个带有数据集的组合框。

            <ComboBox
            Grid.Row="4" 
            Name="AddSubLocationCheckBox"
            Height="40"
            Background="White"
            Visibility="Visible"
            ItemsSource="{x:Bind ListLocations, Mode=OneWay}"
            VerticalAlignment="Top"
            HorizontalAlignment="Stretch"
            PlaceholderText="Set as Sub-Location"
            DisplayMemberPath="Name"
            SelectedItem="{x:Bind SelectedLocation, Mode=TwoWay}"
            Margin="0,30,10,0"/>

如果我选择任何变体,我如何取消选择此组合框中的选择?

1 个答案:

答案 0 :(得分:0)

我创建了一个新按钮&#34;取消&#34;在组合框中取消选择。这是一个简单的方法。我保留了组合框的代码属性和功能,按钮:

           <ComboBox
        Grid.Row="4" 
        Name="AddSubLocationCheckBox"
        Height="40"
        Width="300"
        Background="White"
        Visibility="Visible"
        ItemsSource="{x:Bind ListLocations, Mode=OneWay}"
        VerticalAlignment="Top"
        HorizontalAlignment="Left"
        PlaceholderText="Set as Sub-Location"
        DisplayMemberPath="Name"
        SelectedItem="{x:Bind SelectedLocation, Mode=TwoWay}"
        Margin="0,30,10,0"/>

        <Button
            Grid.Row="4"
            Name="CancelSubLocation"
            HorizontalAlignment="Right"
            VerticalAlignment="Top"
            Width="40"
            Height="40"
            Background="{StaticResource WhiteSolidColorBrush}"
            Tapped="CancelSubLocation_Tapped"
            Visibility="Visible"
            IsEnabled="False"
            Margin="0,30,10,0">
            <FontIcon Glyph="&#xE711;" Foreground="Black"/>
        </Button>

public ComboBoxLocationItem SelectedLocation
{
    get { return (ComboBoxLocationItem)GetValue(SelectedLocationProperty); }
    set { SetValue(SelectedLocationProperty, value); }
}

public static readonly DependencyProperty SelectedLocationProperty =
    DependencyProperty.Register(nameof(SelectedLocation), typeof(ComboBoxLocationItem), typeof(AddLocationControl), new PropertyMetadata(null,
        (o, args) => ((AddLocationControl)o).CheckSubLocation()));

public void CheckSubLocation()
{
        if (SelectedLocation != null)
        {
            CancelSubLocation.IsEnabled = true;
        }
        else
        {
            CancelSubLocation.IsEnabled = false;
        }
}

private void CancelSubLocation_Tapped(object sender, TappedRoutedEventArgs e)
{
    CancelSubLocation.IsEnabled = false;
    SelectedLocation = null;
}