【问题标题】:TemplatedParent not working in MultiBinding: getting "UnsetValue" everytimeTemplatedParent 在 MultiBinding 中不起作用:每次都获取“UnsetValue”
【发布时间】:2016-08-25 14:55:58
【问题描述】:

我正在尝试将样式控件的可见性绑定到其标记等于 TabControl 的选定索引的条件。

我使用 RelativeSource TemplatedParent 但它没有被设置,我怀疑这是因为我没有在 Style 中设置 Template 属性。

这是我的代码:

    <Grid x:Name="Telas" Grid.Column="1">
        <Grid.Resources>
            <vw:TagIsIndexBooleanConverter x:Key="TagIsIndexBooleanConverter"/>
            <Style x:Key="SelectedIndexVisibleStyle" TargetType="UserControl">
                <Setter Property="Visibility" Value="Hidden"/>
                <Style.Triggers>
                    <DataTrigger Value="True">
                        <DataTrigger.Binding>
                            <MultiBinding Converter="{StaticResource TagIsIndexBooleanConverter}">
                                <Binding Path="Tag" RelativeSource="{RelativeSource TemplatedParent}"/>
                                <Binding Path="SelectedIndex" ElementName="menu"/>
                            </MultiBinding>
                        </DataTrigger.Binding>
                        <Setter Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Grid.Resources>

        <vw:TelaColetaView
            x:Name="telaColeta"
            DataContext="{Binding TelaColetaVM}"
            Style="{StaticResource SelectedIndexVisibleStyle}"
            Tag="0"/>

        <vw:TelaPacientesView
            x:Name="telaPacientes"
            DataContext="{Binding TelaPacientesVM}"
            Style="{StaticResource SelectedIndexVisibleStyle}"
            Tag="1"/>

        <vw:TelaConfiguraçõesView
            x:Name="telaConfigurações"
            DataContext="{Binding TelaConfiguraçõesVM}"
            Style="{StaticResource SelectedIndexVisibleStyle}"
            Tag="4"/>

    </Grid>

和转换器:

public class TagIsIndexBooleanConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Any(v => v == null || v == DependencyProperty.UnsetValue))
            return Binding.DoNothing;

        var tag = System.Convert.ToInt32(values[0]);
        var index = System.Convert.ToInt32(values[1]);
        var result = tag == index;
        return result;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

【问题讨论】:

    标签: wpf multibinding wpf-style


    【解决方案1】:

    在这种情况下,您不能使用TemplatedParent,因为没有;这意味着在ControlTemplate 中使用以指定绑定的源是您将模板应用到的控件。

    <Binding Path="Tag" RelativeSource="{RelativeSource TemplatedParent}"/>
    

    但这不在ControlTemplate 中。相反,Tag 只是您正在设计的东西的属性。通常你会为Tag 做一个这样的触发器:

    <Trigger Property="Tag" Value="0">
    

    ...但是您需要多重绑定才能从menu 获取SelectedIndex,并且它必须是MultiDataBinding,因为您需要指定ElementName

    所以你需要一个绑定。要绑定到您自己的属性之一而不是 DataContext 的属性,您可以绑定 RelativeSourceSelf

    <Binding Path="Tag" RelativeSource="{RelativeSource Self}" />
    

    OP 还发现他必须将StyleTargetType 设置为"UserControl",才能使Tag 绑定起作用。

    【讨论】:

    • 要为这个出色的答案添加一点内容,似乎需要在样式中设置 TargetType=UserControl 才能使 &lt;Binding Path="Tag" RelativeSource="{RelativeSource Self}"/&gt; 正常工作。
    • @heltonbiker 补充了这一点,并摧毁了我的 cmets。
    猜你喜欢
    • 2011-03-09
    • 2013-05-30
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 2021-06-04
    • 2019-04-18
    相关资源
    最近更新 更多