英文:
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:
<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>
答案2
得分: 1
可以使用`Style`来为`Border`设置样式,使用`DataTrigger`绑定到父级`DialogCustomControl`,使用`RelativeSource`属性:
<Style TargetType="{x:Type views:DialogCustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type views:DialogCustomControl}">
<Grid>
<Border>
<Border.Style>
<Style TargetType="Border">
<Setter Property="Width" Value="40" />
<Style.Triggers>
<DataTrigger Binding="{Binding IconContent,
RelativeSource={RelativeSource AncestorType=views:DialogCustomControl}}"
Value="{x:Null}">
<Setter Property="Width" Value="0" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<ContentPresenter Content="{TemplateBinding IconContent}" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
英文:
You could use a Style
for the Border
with a DataTrigger
that binds to the parent DialogCustomControl
using the RelativeSource
property:
<Style TargetType="{x:Type views:DialogCustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type views:DialogCustomControl}">
<Grid>
<Border>
<Border.Style>
<Style TargetType="Border">
<Setter Property="Width" Value="40" />
<Style.Triggers>
<DataTrigger Binding="{Binding IconContent,
RelativeSource={RelativeSource AncestorType=views:DialogCustomControl}}"
Value="{x:Null}">
<Setter Property="Width" Value="0" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<ContentPresenter Content="{TemplateBinding IconContent}" />
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论