WPF - 能否将标签的 属性(可见性)绑定到标签的目标?
WPF - Can one Bind a Label's property (Visibility) to the Label's Target?
所以我有许多 XAML 页面,其中包含各种控件,其中大部分都带有指示预期内容的 TextBlock。喜欢:
<TextBlock x:Name="txbCustomerName"
Text="Customer Name"/>
<TextBox x:Name="txtCustomerName"
Text="{Binding DataObject.CustomerName}"/>
我正在用标签替换文本块,它看起来像这样:
<Label x:Name="lblCustomerName"
Content="Customer Name"
Target="{Binding ElementName=txtCustomerName}"/>
<TextBox x:Name="txtCustomerName"
Text="{Binding DataObject.CustomerName}"/>
到目前为止,还不错。但是,有些控件并不总是可见的。因此,关联的 TextBlock 也随之而来:
<TextBlock x:Name="txbInvoiceAddressStreet"
Text="Street Name"
Visibility="{Binding DataObject.DifferentInvoiceAddress, Converter={StaticResource BoolToVisibility}}"/>
<TextBox x:Name="txtInvoiceAddressStreet"
Text="{Binding DataObject.InvoiceAddressStreet}"
Visibility="{Binding DataObject.DifferentInvoiceAddress, Converter={StaticResource BoolToVisibility}}"/>
我或多或少希望 Label 的 Visibility 默认自动等于其 Target 的 Visibility,但显然我必须为此努力。没关系,毕竟是我的工作
初稿很棒:
<Label x:Name="txbInvoiceAddressStreet"
Content="Street Name"
Target="{Binding ElementName=txtInvoiceAddressStreet}"
Visibility="{Binding Path=Visibility, ElementName=txtInvoiceAddressStreet}"/>
<TextBox x:Name="txtInvoiceAddressStreet"
Text="{Binding DataObject.InvoiceAddressStreet}"
Visibility="{Binding DataObject.DifferentInvoiceAddress, Converter={StaticResource BoolToVisibility}}"/>
您会注意到,我的标签可见性的绑定链接到与 Target 相同的元素,而不是与 TextBlock 指向相同的数据元素。我觉得它将相关信息集中在 TextBox 中,而不是将其分散在两个控件上。
一切正常。尽管如此,如果我找到一种方法直接通过标签的目标 属性 将绑定应用到文本框的 属性 而不是重复使用文本框的名称,我还是会觉得我可能会更进一步。
就像这样,但它不起作用,因为源不是依赖项属性:
Visibility="{Binding Path=Visibility, Source={Binding Path=Target, RelativeSource={RelativeSource Self}}}"
正如我所说,这是行不通的。但是,我希望它能传达出我正在努力的意义。
当然,此后的最后一步是将可见性移动到标签的默认样式,所以如果有办法做到这一点,我想了解一下。
在绑定路径中包含目标 属性:
<Label Visibility="{Binding Path=Target.Visibility, RelativeSource={RelativeSource Self}}"/>
它可以是样式的一部分:
<Style TargetType="Label">
<Setter Property="Visibility"
Value="{Binding Path=Target.Visibility, RelativeSource={RelativeSource Self}}"/>
</Style>
所以我有许多 XAML 页面,其中包含各种控件,其中大部分都带有指示预期内容的 TextBlock。喜欢:
<TextBlock x:Name="txbCustomerName"
Text="Customer Name"/>
<TextBox x:Name="txtCustomerName"
Text="{Binding DataObject.CustomerName}"/>
我正在用标签替换文本块,它看起来像这样:
<Label x:Name="lblCustomerName"
Content="Customer Name"
Target="{Binding ElementName=txtCustomerName}"/>
<TextBox x:Name="txtCustomerName"
Text="{Binding DataObject.CustomerName}"/>
到目前为止,还不错。但是,有些控件并不总是可见的。因此,关联的 TextBlock 也随之而来:
<TextBlock x:Name="txbInvoiceAddressStreet"
Text="Street Name"
Visibility="{Binding DataObject.DifferentInvoiceAddress, Converter={StaticResource BoolToVisibility}}"/>
<TextBox x:Name="txtInvoiceAddressStreet"
Text="{Binding DataObject.InvoiceAddressStreet}"
Visibility="{Binding DataObject.DifferentInvoiceAddress, Converter={StaticResource BoolToVisibility}}"/>
我或多或少希望 Label 的 Visibility 默认自动等于其 Target 的 Visibility,但显然我必须为此努力。没关系,毕竟是我的工作
初稿很棒:
<Label x:Name="txbInvoiceAddressStreet"
Content="Street Name"
Target="{Binding ElementName=txtInvoiceAddressStreet}"
Visibility="{Binding Path=Visibility, ElementName=txtInvoiceAddressStreet}"/>
<TextBox x:Name="txtInvoiceAddressStreet"
Text="{Binding DataObject.InvoiceAddressStreet}"
Visibility="{Binding DataObject.DifferentInvoiceAddress, Converter={StaticResource BoolToVisibility}}"/>
您会注意到,我的标签可见性的绑定链接到与 Target 相同的元素,而不是与 TextBlock 指向相同的数据元素。我觉得它将相关信息集中在 TextBox 中,而不是将其分散在两个控件上。
一切正常。尽管如此,如果我找到一种方法直接通过标签的目标 属性 将绑定应用到文本框的 属性 而不是重复使用文本框的名称,我还是会觉得我可能会更进一步。
就像这样,但它不起作用,因为源不是依赖项属性:
Visibility="{Binding Path=Visibility, Source={Binding Path=Target, RelativeSource={RelativeSource Self}}}"
正如我所说,这是行不通的。但是,我希望它能传达出我正在努力的意义。
当然,此后的最后一步是将可见性移动到标签的默认样式,所以如果有办法做到这一点,我想了解一下。
在绑定路径中包含目标 属性:
<Label Visibility="{Binding Path=Target.Visibility, RelativeSource={RelativeSource Self}}"/>
它可以是样式的一部分:
<Style TargetType="Label">
<Setter Property="Visibility"
Value="{Binding Path=Target.Visibility, RelativeSource={RelativeSource Self}}"/>
</Style>