wpf绑定属性的相对路径写法

本文介绍了WPF中使用相对路径进行属性绑定的各种方法,包括如何绑定到祖先类型、自身属性、模板父级以及控件属性,并提到了转换器在绑定中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        <Style x:Key="trumpetBtn" TargetType="{x:Type quJiao:ButtonWithIcon}">
            <Setter Property="Cursor" Value="Hand"/>
            <Setter Property="IsHitTestVisible" Value="True"/>
            <Setter Property="Width" Value="64"/>
            <Setter Property="Height" Value="64"/>
            <Setter Property="IconWidth" Value="64"/>
            <Setter Property="IconHeight" Value="64"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type quJiao:ButtonWithIcon}">
                        <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                            <Image x:Name="img" Source="{TemplateBinding IconPath}" Width="{TemplateBinding IconWidth}" Height="{TemplateBinding IconHeight}"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <DataTrigger Binding="{Binding IsPlaying}" Value="True">
                                <Setter Property="Source" TargetName="img" Value="{Binding Path=SecondIconPath,RelativeSource={RelativeSource AncestorType={x:Type quJiao:ButtonWithIcon}}}"/>
                            </DataTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

Value="{Binding Path=SecondIconPath,RelativeSource={RelativeSource AncestorType={x:Type quJiao:ButtonWithIcon}}}"

如果要在绑定中设置参数,写法如下:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource self},Path=IconPath,Converter={StaticResource BoolConvertObjectIfNull}}" Value="True">
	<Setter TargetName="icon" Property="Visibility" Value="Collapsed"/>
	<Setter TargetName="spacing" Property="Visibility" Value="Collapsed"/>
</DataTrigger>

Binding="{Binding RelativeSource={RelativeSource self},Path=IconPath}"

converter部分是转换器,用来判断IconPath是否为空的

还有一种情况是在style中的resources中的属性,需要绑定到父级,写法如下

<ControlTemplate.Resources>
	<Style TargetType="ContentPresenter">
		<Setter Property="Content" Value="{Binding Path=SelectionBoxItem,RelativeSource={RelativeSource TemplatedParent}}"/>
		<Setter Property="ContentTemplate" Value="{Binding Path=SelectionBoxItemTemplate,RelativeSource={RelativeSource TemplatedParent}}"/>
		<Setter Property="ContentTemplateSelector" Value="{Binding Path=ItemTemplateSelector,RelativeSource={RelativeSource TemplatedParent}}"/>
		<Setter Property="HorizontalAlignment" Value="Left"/>
		<Setter Property="VerticalAlignment" Value="Center"/>
	</Style>
</ControlTemplate.Resources>

Value="{Binding Path=SelectionBoxItem,RelativeSource={RelativeSource TemplatedParent}}"

 

还有一种是绑定到控件属性,看图即可:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值