WPF Telerik RadGridView 可防止在单元格值无效时单击按钮

WPF Telerik RadGridView prevents button clicking when cell value is invalid

提问人:jnpowrie 提问时间:10/28/2023 更新时间:10/28/2023 访问量:16

问:

我有一个带有验证的单元格,可以按预期工作;但是,在单元格无效的情况下,同一窗口中的所有按钮都不会响应。我添加了代码,以便在单元格有效性无效时禁用我的“完成”按钮 ()(见下文),但是无论单元格内容的有效性如何,我的“取消”按钮 () 都应该始终有效。该按钮看起来是可单击的,但即使我在该按钮的“单击”操作中放置了一个断点......它不会触发事件。telerik:RadGridViewbtnDonebtnCancel

ButtonChooser.xaml

<telerik:RadGridView x:Name="gridSelectedControls" CanUserInsertRows="False"
    CanUserDeleteRows="False" CanUserReorderColumns="False"
    SelectionMode="Extended" SelectionUnit="FullRow"
    ItemsSource="{Binding SelectedControls}"
    CellValidating="RadGridViewCellValidating">
    <telerik:GridViewDataColumn x:Name="ValueTextCell" UniqueName="Description"
            DataMemberBinding="{Binding Path=Text, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
            IsReadOnly="False" HeaderCellStyle="{StaticResource HeaderCell}">
            <telerik:GridViewDataColumn.Header>
                <TextBlock x:Name="textBlockLabelText" Text="header" />
            </telerik:GridViewDataColumn.Header>
            <telerik:GridViewDataColumn.CellEditTemplate>
                <DataTemplate>
                    <TextBox x:Name="tbDisplayText" MaxLength="255" Text="TestDataText" />
                </DataTemplate>
            </telerik:GridViewDataColumn.CellEditTemplate>
        </telerik:GridViewDataColumn>
    </telerik:RadGridView.Columns>
</telerik:RadGridView>

<!-- More unrelated code between -->

<StackPanel Orientation="Horizontal" Grid.ColumnSpan="3" Grid.Row="1">
    <Button x:Name="btnDone" Content="Done" IsDefault="True" Click="OnDoneButtonClick" />
    <Button x:Name="btnCancel" Content="Cancel" IsDefault="False" IsCancel="True" Click="OnCancelButtonClick" />
</StackPanel>

按钮选择器.xaml.cs

private void RadGridViewCellValidating(object sender, GridViewCellValidatingEventArgs e)
{
    e.IsValid = true;

    if (string.Compare(e.Cell.Column.UniqueName, "Description") == 0 && e.EditingElement is TextBox element)
    {
        if (string.IsNullOrEmpty(element.Text))
        {
            e.IsValid = false;
            e.ErrorMessage = "Too empty";
        }

        if (element.Text.Length > 255)
        {
            e.IsValid = false;
            e.ErrorMessage = "Too much not empty";
        }

        btnDone.IsEnabled = e.IsValid;
    }
}

任何帮助将不胜感激!🙂

我尝试在按钮的“点击”触发器中放置一个断点,但它们从未触发。我还尝试在 RadGridView 的 和 事件期间添加一个事件以强制按钮,但这也没有做任何事情。BeginningEditcellEditEndedbtnCancel.IsEnable = true

C# WPF 验证 XAML Telerik

评论


答: 暂无答案