模板化数据模板

发布于 2024-12-04 21:31:06 字数 2561 浏览 0 评论 0原文

我正在创建一个带有模板项(DataTemplate)的列表框。在此模板中,我希望能够根据与其绑定的数据来更改模板的一部分的呈现方式。这是我写的:

    <ListBox Grid.Row="1"
        ItemsSource="{Binding Indices}"
        HorizontalContentAlignment="Stretch">
        <DataTemplate>
            <Grid Margin="3">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="4*"/>
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <!-- ****this does not work ;(**** -->
                <ContentPresenter Grid.Column="0"
                    Content="{Binding}">
                    <ContentPresenter.ContentTemplateSelector>
                        <tmpl:BoolBasedSelector
                            Value="{Binding IsEditable, ElementName=MainCtrl}"
                            TrueTemplate="{StaticResource listBoxEditableLabel}"
                            FalseTemplate="{StaticResource listBoxNonEditableLabel}" />
                    </ContentPresenter.ContentTemplateSelector>
                </ContentPresenter>

                <!-- other stuff, not important right now -->
                <Button Grid.Column="1" />
                <Button Grid.Column="2" />
            </Grid>
        </DataTemplate>
    </ListBox>

MainCtrl 是包含此列表的网格所在的主控件的名称。 如何使用可交换网格的元素编写ListBox的DataTamplate?谢谢你的建议。

编辑: tmpl:BoolBasedSelector非常简单:

class BoolBasedSelector: DataTemplateSelector {
    public bool Value { get; set; }
    public DataTemplate TrueTemplate { get; set; }
    public DataTemplate FalseTemplate { get; set; }

    public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container) {
        return Value ? TrueTemplate : FalseTemplate;
    }
}

引用的资源如下所示:

<DataTemplate x:Key="listBoxEditableLable">
    <TextBox Text="{Binding Label}" />
</DataTemplate>

<DataTemplate x:Key="listBoxNonEditableLable">
    <TextBlock Text="{Binding Label}" />
</DataTemplate>

ListBox绑定的DataContext的Indices属性:

interface IIndex {
    string Label { get; set; }
    IIndexValueProvider ValueProvider { get; set; }
    IIndexValidator Validator { get; set; }
    bool IsEditable { get; set; }
    bool IsGrouped { get; set; }
}

I'm creating a ListBox with templated items (DataTemplate). Inside this template I want to have the possibility of changing the way a part of the template is presented depending of the data bounded to it. Here's what I wrote:

    <ListBox Grid.Row="1"
        ItemsSource="{Binding Indices}"
        HorizontalContentAlignment="Stretch">
        <DataTemplate>
            <Grid Margin="3">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="4*"/>
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <!-- ****this does not work ;(**** -->
                <ContentPresenter Grid.Column="0"
                    Content="{Binding}">
                    <ContentPresenter.ContentTemplateSelector>
                        <tmpl:BoolBasedSelector
                            Value="{Binding IsEditable, ElementName=MainCtrl}"
                            TrueTemplate="{StaticResource listBoxEditableLabel}"
                            FalseTemplate="{StaticResource listBoxNonEditableLabel}" />
                    </ContentPresenter.ContentTemplateSelector>
                </ContentPresenter>

                <!-- other stuff, not important right now -->
                <Button Grid.Column="1" />
                <Button Grid.Column="2" />
            </Grid>
        </DataTemplate>
    </ListBox>

MainCtrl is the name of the main control within which the grid containing this list is placed.
How do I write ListBox's DataTamplate with exchangeable grid's element? Thanks for you advices.

EDIT:
tmpl:BoolBasedSelector is really simple:

class BoolBasedSelector: DataTemplateSelector {
    public bool Value { get; set; }
    public DataTemplate TrueTemplate { get; set; }
    public DataTemplate FalseTemplate { get; set; }

    public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container) {
        return Value ? TrueTemplate : FalseTemplate;
    }
}

The referenced resources are like this:

<DataTemplate x:Key="listBoxEditableLable">
    <TextBox Text="{Binding Label}" />
</DataTemplate>

<DataTemplate x:Key="listBoxNonEditableLable">
    <TextBlock Text="{Binding Label}" />
</DataTemplate>

Indices property of the DataContext to which ListBox binds:

interface IIndex {
    string Label { get; set; }
    IIndexValueProvider ValueProvider { get; set; }
    IIndexValidator Validator { get; set; }
    bool IsEditable { get; set; }
    bool IsGrouped { get; set; }
}

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

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

发布评论

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

评论(1

又爬满兰若 2024-12-11 21:31:06

您的模板使用静态资源名称“listBoxEditableLable”和“listBoxNonEditableLabel”,但您的数据模板键是“listBoxEditableLable”和“listBoxNonEditableLable”。

注意标签的不同拼写。 “标签”与“标签”

Your template uses the static resource names "listBoxEditableLable" and "listBoxNonEditableLabel", but your data template keys are "listBoxEditableLable" and "listBoxNonEditableLable".

Note the different spellings of label. "Label" vs "Lable"

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