WPF 绑定 Grid.Column 属性

发布于 2024-10-26 00:40:14 字数 905 浏览 7 评论 0原文

我需要通过转换器设置项目的 Grid.Column 属性。 这是我的 xaml:

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Periodo.Inizio}">
            <Grid.Column>
                <MultiBinding Converter="{StaticResource ItemColumnSetter}">
                    <Binding RelativeSource="{RelativeSource Self}" />
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="StartDate" />
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="EndDate" />
                    <Binding Path="Periodo.Inizio" />
                </MultiBinding>
            </Grid.Column>
        </TextBlock>
    </DataTemplate>
</ItemsControl.ItemTemplate>

但不起作用。 我确信转换器工作得很好......

I need to set Grid.Column property of an item by a converter.
this is my xaml :

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Periodo.Inizio}">
            <Grid.Column>
                <MultiBinding Converter="{StaticResource ItemColumnSetter}">
                    <Binding RelativeSource="{RelativeSource Self}" />
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="StartDate" />
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="EndDate" />
                    <Binding Path="Periodo.Inizio" />
                </MultiBinding>
            </Grid.Column>
        </TextBlock>
    </DataTemplate>
</ItemsControl.ItemTemplate>

But don't work.
I'm sure that the converter work well...

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

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

发布评论

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

评论(1

南薇 2024-11-02 00:40:14

您的 TextBlock 将被包装在某种类型的另一个控件中,这意味着任何 Grid.XXX 属性都将被忽略。要正确应用这些,您需要在 ItemsControl.ItemContainerStyle 中进行绑定。

应该是这样的:

<ItemsControl.ItemContainerStyle>
    <Style>
        <Setter Property="Grid.Column">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource ItemColumnSetter}">
                    <Binding RelativeSource="{RelativeSource Self}" />
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="StartDate" />
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="EndDate" />
                    <Binding Path="Periodo.Inizio" />
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>
</ItemsControl.ItemContainerStyle>

Your TextBlock will be wrapped in another control of some sort, that means any Grid.XXX properties will be disregarded. To apply those properly you need to do the binding in the ItemsControl.ItemContainerStyle.

Should be something like this:

<ItemsControl.ItemContainerStyle>
    <Style>
        <Setter Property="Grid.Column">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource ItemColumnSetter}">
                    <Binding RelativeSource="{RelativeSource Self}" />
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="StartDate" />
                    <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=local:Timeline}" Path="EndDate" />
                    <Binding Path="Periodo.Inizio" />
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>
</ItemsControl.ItemContainerStyle>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文