【问题标题】:Error: "This operation is valid only on elements that have this template applied"错误:“此操作仅对应用此模板的元素有效”
【发布时间】:2016-07-29 23:21:57
【问题描述】:

我有一个包含用户控件的 C# WPF 应用程序:

<UserControl
        x:Name="payrollEntryControl"
        x:Class="MyNamespace.PayrollEntryControl"
        [...]
        >
    [...]
</UserControl>

在用户控件中,我有一个 Telerik RadDataForm

<telerik:RadDataForm
        x:Name="payrollAddForm"
        CurrentItem="[...]"
        EditTemplate="{StaticResource myEditTemplate}"
        />

模板包含 Telerik RadGridViewButton

<telerik:RadGridView Grid.Row="0" Grid.Column="0"
        x:Name="workGridView"
        [...]
        ItemsSource="{Binding [...]}"
        >
    <telerik:RadGridView.Columns>
        [...]
    </telerik:RadGridView.Columns>
</telerik:RadGridView>
<Button Grid.Row="1" Grid.Column="0"
        Command="{Binding addWorkCommand, ElementName=payrollEntryControl}"
        >
    Add
</Button>

我想要的命令是在workGridView 上调用BeginInsert()。但我似乎无法访问workGridView

到目前为止,我的命令:

private DelegateCommand addWorkCommand_ = null;
public DelegateCommand addWorkCommand
{
    get
    {
        if (this.addWorkCommand_ == null)
        {
            this.addWorkCommand_ = new DelegateCommand(
                o => addWork(o)
            );
        }

        return this.addWorkCommand_;
    }
}

private void addWork(object o)
{
    var addForm = this.payrollAddForm;
    var editTemplate = addForm.EditTemplate;
    var workGrid = editTemplate.FindName("workGridView", addForm);
}

我的问题?当我拨打editTemplate.FindName() 时,我得到一个例外:

此操作仅对应用了此模板的元素有效。

我不明白。我正在从表单中获取模板。怎么可能不适用?

【问题讨论】:

  • 这个问题似乎对 Telerik 库非常具体。您可能需要专门从熟悉该供应商产品的人那里获得帮助。除此之外,如果没有良好的minimal reproducible example,任何人都可能难以直接诊断问题。请说明您在调试和研究错误消息方面已经尝试过什么(包括返回的项目的相关性here)。

标签: c# wpf telerik


【解决方案1】:

Peter Duniho 的评论将我指向 this 答案,它解决了我的问题。

这个方法对你有帮助:

public T FindElementByName<T>(FrameworkElement element, string sChildName) where T : FrameworkElement
{
    T childElement = null;
    var nChildCount = VisualTreeHelper.GetChildrenCount(element);
    for (int i = 0; i < nChildCount; i++)
    {
        FrameworkElement child = VisualTreeHelper.GetChild(element, i) as FrameworkElement;

        if (child == null)
            continue;

        if (child is T && child.Name.Equals(sChildName))
        {
            childElement = (T)child;
            break;
        }

        childElement = FindElementByName<T>(child, sChildName);

        if (childElement != null)
            break;
    }

    return childElement;
}

而且,我如何使用它,只需添加按钮,然后单击按钮:

private void Button_OnClick(object sender, RoutedEventArgs e)
{
    var element = FindElementByName<ComboBox>(ccBloodGroup, "cbBloodGroup");
}
 [1]: https://stackoverflow.com/a/19907800/243563

另一种方法是将 workGridView 作为 CommandParameter 传递:

<Button Grid.Row="1" Grid.Column="0"
    CommandParameter="{Binding ElementName=workGridView}"
    Command="{Binding addWorkCommand}" >
 ....

private void addWork(object o)
{
    RadGridView grid = o as RadGridView;
    grid.BeginInsert();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多