将数据模板选择器绑定到内容控制模板

发布于 2024-12-08 06:45:31 字数 484 浏览 4 评论 0原文

我想将内容控件绑定到数据模板选择器,将变量绑定到该内容控件,然后根据变量包含的内容显示不同的模板。

我已经设法让 Telerik DataTemplateSelector 来执行我需要的操作,但是我找不到将 DataTemplateSelector 绑定到的控件。

我发现允许我使用 DataTemplateSelector 的所有控件都要求 ItemsSource 是一个集合,如果我传入单个对象,它会引发异常。

我传递的项目是单个项目而不是集合。内容控件似乎是我所需要的,但我无法将 DataTemplateSelector 绑定到它。

是否有类似于内容控件的 telerik 控件,我可以将 DataTemplateSelector 绑定到该控件?或者是否有类似于 DataTemplateSelector 的东西,我可以将其绑定到内容控件。

任何帮助将不胜感激。

I would like to bind a content control to a data template selector, bind a variable to that content control and then display a different template depending on what the variable contains.

I've managed to get a Telerik DataTemplateSelector to do what I need it to, however I can't find a control to bind the DataTemplateSelector to.

All of the controls that I've found that allow me to use a DataTemplateSelector require the ItemsSource to be a collection, if I pass in a single object it throws an exception.

The item I'm passing in is a single item not a collection. The content control seems to be what I need but i can't bind a DataTemplateSelector to it.

Is there a telerik control similar to the Content Control, that I can bind a DataTemplateSelector to? Or is there something similar to a DataTemplateSelector that I can bind to a Content Control.

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦纸 2024-12-15 06:45:31

我认为你应该考虑避免 Telerik 的课程(在我看来,在这种情况下,这会让事情变得有点复杂)。

标准 DataTemplateSelector 实现怎么样?
自己实现非常容易!

首先,您声明抽象DataTemplateSelector的“经典”实现:

public abstract class DataTemplateSelector : ContentControl
{
    public virtual DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        return null;
    }

    protected override void OnContentChanged(object oldContent, object newContent)
    {
        base.OnContentChanged(oldContent, newContent);

        ContentTemplate = SelectTemplate(newContent, this);
    }
}

然后您可以编写自定义DataTemplateSelector......

public class myTemplateSelector : DataTemplateSelector
{
    public DataTemplate Template1 { get; set; }

    public DataTemplate Template2 { get; set; }


    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        // No template...
        if (item == null)
            return null;

        // Enumeration discriminant:
        if (item is BoundTemplateDiscriminantType)
            switch ((BoundTemplateDiscriminantType)item)
            {
                case BoundTemplateDiscriminantType.Type1:
                    return Template1;
                case BoundTemplateDiscriminantType.Type2:
                    return Template2;
                // Not implemented...
                default:
                    throw new NotImplementedException();
            }
        // Integer discriminant:
        else if (item is int)
        {
            return (int)item > 0 ? Template1 : Template2;
        }
        // Other discriminants...
        else
            // Not yet implemented...
            throw new NotImplementedException();
    }
}

最后XAML 设计(不是通常的 ListBoxItem,而是本例中的可滚动内容):

<ScrollViewer>
    <ScrollViewer.ContentTemplate>
        <DataTemplate>
            <utilities:myTemplateSelector Content="{Binding Path=BoundDiscriminant, Mode=OneWay}">
                <!--Content of first template...-->
                <utils:myTemplateSelector.Template1>
                    <DataTemplate>
                          <TextBlock Text={Binding Path=BoundDescription, Mode=OneWay} />              
                    </DataTemplate>
                </utils:myTemplateSelector.Template1>
                <!--Content of second template...-->
                <utils:myTemplateSelector.Template2>
                    <DataTemplate>

                    </DataTemplate>
                </utils:myTemplateSelector.Template2>
                ... and so on...
            </utilities:myTemplateSelector>
        </DataTemplate>
    </ScrollViewer.ContentTemplate>
</ScrollViewer>

I think you should consider avoiding Telerik's classes (which, in my opinion, complicate the things a bit in this case).

What about a standard DataTemplateSelector implementation?
It's very easy to implement by yourself!

First you declare a "classic" implementation of the abstract class DataTemplateSelector:

public abstract class DataTemplateSelector : ContentControl
{
    public virtual DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        return null;
    }

    protected override void OnContentChanged(object oldContent, object newContent)
    {
        base.OnContentChanged(oldContent, newContent);

        ContentTemplate = SelectTemplate(newContent, this);
    }
}

Then you can write you custom DataTemplateSelector...

public class myTemplateSelector : DataTemplateSelector
{
    public DataTemplate Template1 { get; set; }

    public DataTemplate Template2 { get; set; }


    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        // No template...
        if (item == null)
            return null;

        // Enumeration discriminant:
        if (item is BoundTemplateDiscriminantType)
            switch ((BoundTemplateDiscriminantType)item)
            {
                case BoundTemplateDiscriminantType.Type1:
                    return Template1;
                case BoundTemplateDiscriminantType.Type2:
                    return Template2;
                // Not implemented...
                default:
                    throw new NotImplementedException();
            }
        // Integer discriminant:
        else if (item is int)
        {
            return (int)item > 0 ? Template1 : Template2;
        }
        // Other discriminants...
        else
            // Not yet implemented...
            throw new NotImplementedException();
    }
}

...and finally the XAML designing (not the usual ListBoxItem, but a scrollable content in this case):

<ScrollViewer>
    <ScrollViewer.ContentTemplate>
        <DataTemplate>
            <utilities:myTemplateSelector Content="{Binding Path=BoundDiscriminant, Mode=OneWay}">
                <!--Content of first template...-->
                <utils:myTemplateSelector.Template1>
                    <DataTemplate>
                          <TextBlock Text={Binding Path=BoundDescription, Mode=OneWay} />              
                    </DataTemplate>
                </utils:myTemplateSelector.Template1>
                <!--Content of second template...-->
                <utils:myTemplateSelector.Template2>
                    <DataTemplate>

                    </DataTemplate>
                </utils:myTemplateSelector.Template2>
                ... and so on...
            </utilities:myTemplateSelector>
        </DataTemplate>
    </ScrollViewer.ContentTemplate>
</ScrollViewer>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文