WPF模板绑定,根据另一个元素中内容的存在来设置元素的宽度。

huangapple go评论58阅读模式
英文:

WPF template binding for a width of element based on presence of content in another element

问题

我需要它的宽度为40,如果IconContent不为空,否则为0。 我知道如何在代码中使用依赖属性使其工作。但我相当确定也可以以声明方式实现。

英文:

I have custom control (not user control) which inherits from ContentControl.
Its default style:

    <Style TargetType="{x:Type views:DialogCustomControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type views:DialogCustomControl}">
                    <Grid>
                        <Border Width="40">
                            <ContentPresenter Content="{TemplateBinding IconContent}" />
                        </Border>

I need it to be 40 if IconContent is not null and 0 otherwise.

I know how to make it work in code with dep prop. But I am pretty sure it is also possible declaratively.

答案1

得分: 1

你可以将DataTrigger添加到你的Border的Style中,并将[x:Null]用作其值:

<Border>
    <Border.Style>
        <Style TargetType="Border">
            <Setter Property="Width" Value="40" />
            <Style.Triggers>
                <DataTrigger Binding="{TemplateBinding IconContent}" Value="{x:Null}">
                    <Setter Property="Width" Value="0" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Border.Style>
    <ContentPresenter Content="{TemplateBinding IconContent}" />
</Border>

详细信息

英文:

You can add a DataTrigger to your Border's Style and use x:Null as its Value:

&lt;Border&gt;
    &lt;Border.Style&gt;
        &lt;Style TargetType=&quot;Border&quot;&gt;
            &lt;Setter Property=&quot;Width&quot;
                    Value=&quot;40&quot; /&gt;
            &lt;Style.Triggers&gt;
                &lt;DataTrigger Binding=&quot;{TemplateBinding IconContent}&quot;
                                Value=&quot;{x:Null}&quot;&gt;
                    &lt;Setter Property=&quot;Width&quot;
                            Value=&quot;0&quot; /&gt;
                &lt;/DataTrigger&gt;
            &lt;/Style.Triggers&gt;
        &lt;/Style&gt;
    &lt;/Border.Style&gt;
    &lt;ContentPresenter Content=&quot;{TemplateBinding IconContent}&quot; /&gt;
&lt;/Border&gt;

答案2

得分: 1

可以使用`Style`来为`Border`设置样式,使用`DataTrigger`绑定到父级`DialogCustomControl`,使用`RelativeSource`属性:

    &lt;Style TargetType=&quot;{x:Type views:DialogCustomControl}&quot;&gt;
        &lt;Setter Property=&quot;Template&quot;&gt;
            &lt;Setter.Value&gt;
                &lt;ControlTemplate TargetType=&quot;{x:Type views:DialogCustomControl}&quot;&gt;
                    &lt;Grid&gt;
                        &lt;Border&gt;
                            &lt;Border.Style&gt;
                                &lt;Style TargetType=&quot;Border&quot;&gt;
                                    &lt;Setter Property=&quot;Width&quot; Value=&quot;40&quot; /&gt;
                                    &lt;Style.Triggers&gt;
                                        &lt;DataTrigger Binding=&quot;{Binding IconContent, 
                                            RelativeSource={RelativeSource AncestorType=views:DialogCustomControl}}&quot;
                                                     Value=&quot;{x:Null}&quot;&gt;
                                            &lt;Setter Property=&quot;Width&quot; Value=&quot;0&quot; /&gt;
                                        &lt;/DataTrigger&gt;
                                    &lt;/Style.Triggers&gt;
                                &lt;/Style&gt;
                            &lt;/Border.Style&gt;
                            &lt;ContentPresenter Content=&quot;{TemplateBinding IconContent}&quot; /&gt;
                        &lt;/Border&gt;
                    &lt;/Grid&gt;
                &lt;/ControlTemplate&gt;
            &lt;/Setter.Value&gt;
        &lt;/Setter&gt;
    &lt;/Style&gt;
英文:

You could use a Style for the Border with a DataTrigger that binds to the parent DialogCustomControl using the RelativeSource property:

&lt;Style TargetType=&quot;{x:Type views:DialogCustomControl}&quot;&gt;
    &lt;Setter Property=&quot;Template&quot;&gt;
        &lt;Setter.Value&gt;
            &lt;ControlTemplate TargetType=&quot;{x:Type views:DialogCustomControl}&quot;&gt;
                &lt;Grid&gt;
                    &lt;Border&gt;
                        &lt;Border.Style&gt;
                            &lt;Style TargetType=&quot;Border&quot;&gt;
                                &lt;Setter Property=&quot;Width&quot; Value=&quot;40&quot; /&gt;
                                &lt;Style.Triggers&gt;
                                    &lt;DataTrigger Binding=&quot;{Binding IconContent, 
                                        RelativeSource={RelativeSource AncestorType=views:DialogCustomControl}}&quot;
                                                 Value=&quot;{x:Null}&quot;&gt;
                                        &lt;Setter Property=&quot;Width&quot; Value=&quot;0&quot; /&gt;
                                    &lt;/DataTrigger&gt;
                                &lt;/Style.Triggers&gt;
                            &lt;/Style&gt;
                        &lt;/Border.Style&gt;
                        &lt;ContentPresenter Content=&quot;{TemplateBinding IconContent}&quot; /&gt;
                    &lt;/Border&gt;
                &lt;/Grid&gt;
            &lt;/ControlTemplate&gt;
        &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
&lt;/Style&gt;

huangapple
  • 本文由 发表于 2023年5月22日 20:57:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定