在Xaml中根据ComboBox的选择启用/禁用控件

9

如何在选择/未选择组合框时启用/禁用诸如文本框、标签、文本块等控件?例如,如果选择的索引大于零,则启用控件,否则禁用。如何将控件的IsEnabled属性与组合框选择绑定?


你绑定了IsEnabled。 - paparazzo
@Paparazzi 我更新了问题。 - xyz
ComboboxSelectionPropertyChanged事件中,您需要更新绑定到要启用/禁用的控件的IsEnabled属性的VM中的某些其他属性。 - Kidiskidvogingogin
3个回答

14

您可以将IsEnabled绑定到ComboBox的SelectedIndex属性,并使用IValueConverter将其转换为布尔值。例如,在XAML中(显示启用Button):

<ComboBox x:Name="cmbBox" ItemsSource="{Binding Source={StaticResource DataList}}"/>
<Button Grid.Column="1" IsEnabled="{Binding ElementName=cmbBox, Path=SelectedIndex, Converter={StaticResource IndexToBoolConverter}}"/>

那么您还需要一个转换器,例如:

public class IndexToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if ((int)value > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

你还需要将 Converter 声明为资源,例如在你的 Window 中。

<local:IndexToBoolConverter x:Key="IndexToBoolConverter"/>

7
我会做类似于这样的事情。
<Grid>
    <Grid.Resources>
        <Style TargetType="{x:Type Button}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=SelectedItem, 
                                               ElementName=TheCombo}" 
                                               Value="{x:Null}">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Grid.Resources>

    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">

        <ComboBox x:Name="TheCombo" Width="100">
            <ComboBoxItem>Blah</ComboBoxItem>
            <ComboBoxItem>Blah</ComboBoxItem>
            <ComboBoxItem>Blah</ComboBoxItem>
        </ComboBox>

        <Button Content="Click Me" Margin="0,10"/>

    </StackPanel>

</Grid>

希望这能帮到你,加油!

0

试试这个

Dispatcher.BeginInvoke(new Action(() =>
            {
                ToggleButton dropDownButton = GetFirstChildOfType<ToggleButton>(cboMedicos);
                if (dropDownButton != null)
                {
                    dropDownButton.IsEnabled = false;
                }

            }), System.Windows.Threading.DispatcherPriority.Render);


    public static T GetFirstChildOfType<T>(DependencyObject dependencyObject) where T : DependencyObject
    {
        if (dependencyObject == null)
        {
            return null;
        }

        for (var i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
        {
            var child = VisualTreeHelper.GetChild(dependencyObject, i);

            var result = (child as T) ?? GetFirstChildOfType<T>(child);

            if (result != null)
            {
                return result;
            }
        }

        return null;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接