如何在WPF中选择组合框时禁用扩展器?

时间:2018-09-28 04:07:33

标签: c# wpf xaml

实际上,我是wpf的新手。我想在组合框选择上禁用扩展器。当所选索引= 0时,应禁用扩展器,否则应启用。 I tried this both suggestions,但在第一个答案中,我对此有误。行==>

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

和第二个答案,但这会干扰扩展器的外观(样式)。

我的Xaml代码是:

      <UserControl x:Class="ABC.UI.Pages.Scan.Create.Base"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                     xmlns:mui="http://firstfloorsoftware.com/ModernUI"

                     mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="500">

            <Grid Style="{StaticResource ContentRoot}">
                <Grid.Resources>
                    <Style x:Key="ExpanderDriven" TargetType="{x:Type Expander}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=SelectedIndex,
                                                       ElementName=ComboboxMode}"
                                                       Value="0">
                                <Setter Property="IsEnabled" Value="False" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                    <Style x:Key="ExpanderDriven1" TargetType="{x:Type Expander}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Path=SelectedIndex,
                                                       ElementName=ComboboxMode}"
                                                       Value="0">
                                <Setter Property="IsEnabled" Value="True" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Grid.Resources>

 <Expander Name="ExpanderDriven1" Header="Driven properties" Margin="0 5 0 0" Style="{StaticResource ExpanderDriven}" IsEnabled="{Binding ElementName=cmbBox, Path=SelectedIndex, Converter={StaticResource IndexToBoolConverter}}"
                          BorderBrush="{DynamicResource WindowBorder}"
                          Background="{DynamicResource WindowBorder}">
                    <StackPanel>
                        <Grid>

                            <Label Content="Driving Tag" />
</Grid> </StackPanel></Expander>

1 个答案:

答案 0 :(得分:0)

这是因为您需要像这样将其添加到ResourceDictionary:

class PatientRegisterView(LoginRequiredMixin, CreateView): model = Patient form_class = PatientRegisterForm template_name = 'patients/register.html' success_url = 'accounts:dashboard' def patient_eligible(self, test): if not test.is_eligible: return False else: return True def form_valid(self, form): form.instance.provider = self.request.user test = form.save(commit=False) if self.patient_eligible(test): messages.success(self.request, 'Patient registered successfully!') test.save() return super().form_valid(form) # fails here if eligible else: messages.error(self.request, 'Patient not registered') return redirect(self.request, 'accounts:dashboard') # fails here if not eligible

回答– @ vasily.sib

及其工作!