按钮已禁用-DialogHost和日历

时间:2018-08-15 15:20:16

标签: c# wpf xaml dialog material-design-in-xaml

因此由于某种原因,我无法启用该按钮。我正在Pickers用户控件(MaterialDesignInXamlToolkit中使用倒数第二个示例 )-https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/master/MainDemo.Wpf/Pickers.xaml

enter image description here 根据所附的图片,这是我在运行以下代码(原始)时看到的内容:

<StackPanel Orientation="Horizontal" Margin="0 25 0 15" HorizontalAlignment="Center" IsEnabled="True">
                <TextBlock Foreground="{DynamicResource MaterialDesignBodyLight}" Text="{Binding Date, StringFormat=d}" VerticalAlignment="Center" FontSize="24" />
                <Button Margin="8 0 0 0" Content="..."
                        IsEnabled="True"
                        materialDesign:DialogHost.DialogOpenedAttached="CalendarDialogOpenedEventHandler"
                        materialDesign:DialogHost.DialogClosingAttached="CalendarDialogClosingEventHandler"
                        Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}">
                    <Button.CommandParameter>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Calendar x:Name="Calendar" Margin="-1 -4 -1 0" />
                            <StackPanel Grid.Row="1" Margin="8" HorizontalAlignment="Right" Orientation="Horizontal">
                                <Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" IsDefault="True" CommandParameter="1" Style="{DynamicResource MaterialDesignFlatButton}">OK</Button>
                                <Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" IsCancel="True" CommandParameter="0" Style="{DynamicResource MaterialDesignFlatButton}">CANCEL</Button>
                            </StackPanel>
                        </Grid>
                    </Button.CommandParameter>
                </Button>

我确实看过这篇类似的文章,但没有获奖-MaterialDesignXamlToolkit wpf dialoghost buttons are disabled when opened

xaml.cs:

        public void CalendarDialogOpenedEventHandler(object sender, DialogOpenedEventArgs eventArgs)
    {
        Calendar.SelectedDate = ((ClientIntroViewModel)DataContext).Date;
    }

    public void CalendarDialogClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
    {
        if (!Equals(eventArgs.Parameter, "1")) return;

        if (!Calendar.SelectedDate.HasValue)
        {
            eventArgs.Cancel();
            return;
        }

        ((ClientIntroViewModel)DataContext).Date = Calendar.SelectedDate.Value;
    }

最新的XAML:

<StackPanel Orientation="Horizontal" Margin="0 25 0 0" HorizontalAlignment="Center" IsEnabled="True">
                <Label Foreground="{DynamicResource MaterialDesignBodyLight}" VerticalAlignment="Center" Margin="0,0,5,0">Birth Date:</Label>
                <TextBlock Foreground="{DynamicResource MaterialDesignBodyLight}" Text="{Binding Date, StringFormat=d}" VerticalAlignment="Center" FontSize="24" />
                <Button Margin="8 0 0 0" Content="..."
                IsEnabled="True"
                materialDesign:DialogHost.DialogOpenedAttached="CalendarDialogOpenedEventHandler"
                materialDesign:DialogHost.DialogClosingAttached="CalendarDialogClosingEventHandler"
                Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}"
                CommandTarget="{Binding ElementName=PopupAddCustom}">
                </Button>

                <materialDesign:DialogHost Name="PopupAddCustom" HorizontalAlignment="Center" VerticalAlignment="Center" IsOpen="False" >
                    <materialDesign:DialogHost.DialogContent>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <Calendar x:Name="Calendar" Margin="-1 -4 -1 0" />
                            <StackPanel Grid.Row="1" Margin="8" HorizontalAlignment="Right" Orientation="Horizontal">
                                <Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" IsDefault="True"
                    CommandParameter="1" Style="{DynamicResource MaterialDesignFlatButton}">OK</Button>
                                <Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" IsCancel="True"
                    CommandParameter="0" Style="{DynamicResource MaterialDesignFlatButton}">CANCEL</Button>
                            </StackPanel>
                        </Grid>
                    </materialDesign:DialogHost.DialogContent>
                </materialDesign:DialogHost>
            </StackPanel>

1 个答案:

答案 0 :(得分:1)

您没有在CommandTarget上设置Button属性,因此未启用该属性。

<Button Margin="8 0 0 0" Content="..."
        IsEnabled="True"
        Command="{x:Static materialDesign:DialogHost.OpenDialogCommand}"
        CommandTarget="{Binding ElementName=PopupAddCustom}">
</Button>

<materialDesign:DialogHost Name="PopupAddCustom" HorizontalAlignment="Center" VerticalAlignment="Center" IsOpen="False"
                        materialDesign:DialogHost.DialogOpenedCallback="CalendarDialogOpenedEventHandler"
                        materialDesign:DialogHost.DialogClosingCallback="CalendarDialogClosingEventHandler">
    <materialDesign:DialogHost.DialogContent>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Calendar x:Name="Calendar" Margin="-1 -4 -1 0" />
            <StackPanel Grid.Row="1" Margin="8" HorizontalAlignment="Right" Orientation="Horizontal">
                <Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" IsDefault="True"
                        CommandParameter="1" Style="{DynamicResource MaterialDesignFlatButton}">OK</Button>
                <Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" IsCancel="True"
                        CommandParameter="0" Style="{DynamicResource MaterialDesignFlatButton}">CANCEL</Button>
            </StackPanel>
        </Grid>
    </materialDesign:DialogHost.DialogContent>
</materialDesign:DialogHost>