访问 WPF 中 TextBlock 的值绑定 TextProperty

发布于 2024-10-16 15:42:10 字数 732 浏览 4 评论 0原文

我有两个 TextBlock,其 TextProperty 绑定到类中的两个属性。即 StackingText 和 StackingRate... 我希望如果 StackingRate 为 null 或空,则 StackingText 应设置为 null 或空,因为 StackingText 的默认值是“收费”。

<TextBlock Name="stackingText"
           Margin="270,215,0,0"
           Text="{Binding Path=StackingText}"
           HorizontalAlignment="Left"
           VerticalAlignment="Top"
           FontSize="14"
           Padding="5"/>
<TextBlock Name="stackingRate"
           Margin="270,215,0,0"
           Text="{Binding Path=StackingRate}"
           HorizontalAlignment="Left"
           VerticalAlignment="Top"
           FontSize="14"
           Padding="5"/>

实际上我正在打印此页面,因此当 stackingRate 为空时, stackingText 具有值是没有意义的。

I have two TextBlock whose TextProperty is binded to two properties in class. Namely, StackingText and StackingRate...
I want that if StackingRate is null or empty, the StackingText should be set to null or empty since the default value for StackingText is "Is charged at".

<TextBlock Name="stackingText"
           Margin="270,215,0,0"
           Text="{Binding Path=StackingText}"
           HorizontalAlignment="Left"
           VerticalAlignment="Top"
           FontSize="14"
           Padding="5"/>
<TextBlock Name="stackingRate"
           Margin="270,215,0,0"
           Text="{Binding Path=StackingRate}"
           HorizontalAlignment="Left"
           VerticalAlignment="Top"
           FontSize="14"
           Padding="5"/>

Actually i am printing this page, so it doesn't make sense that when stackingRate is empty, stackingText has value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

城歌 2024-10-23 15:42:10

您可以将 DataTrigger 用于 stackingText TextBlock,在其中绑定到 stackingRate `TextBlock' 的文本,如果值为“”(null 或空),则设置也发短信给“”。由于触发器的工作方式,这也将使属性 StackingText 保持不变

<TextBlock Name="stackingText"
           Margin="270,215,0,0"
           HorizontalAlignment="Left"
           VerticalAlignment="Top"
           FontSize="14"
           Padding="5">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Text" Value="{Binding Path=StackingText}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=stackingRate, Path=Text}"
                             Value="">
                    <Setter Property="Text" Value=""/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
<TextBlock Name="stackingRate"
           Margin="270,215,0,0"
           Text="{Binding Path=StackingRate}"
           HorizontalAlignment="Left"
           VerticalAlignment="Top"
           FontSize="14"
           Padding="5"/>

You could use a DataTrigger for the stackingText TextBlock where you bind to Text for the stackingRate `TextBlock' and if the Value is "" (null or empty) then you set the Text to "" as well. This will also leave the property StackingText untouched because of the way that Triggers work

<TextBlock Name="stackingText"
           Margin="270,215,0,0"
           HorizontalAlignment="Left"
           VerticalAlignment="Top"
           FontSize="14"
           Padding="5">
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Text" Value="{Binding Path=StackingText}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=stackingRate, Path=Text}"
                             Value="">
                    <Setter Property="Text" Value=""/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>
<TextBlock Name="stackingRate"
           Margin="270,215,0,0"
           Text="{Binding Path=StackingRate}"
           HorizontalAlignment="Left"
           VerticalAlignment="Top"
           FontSize="14"
           Padding="5"/>
黯然#的苍凉 2024-10-23 15:42:10

正如 Melak 指出的那样,您可以通过触发器来完成此操作。不过,如果您使用 MVVM,您甚至不需要问这个问题。

我使用 WPF 的次数越多,我就越清楚,如果您只实现一个视图模型,一切都会变得更简单。 写一些东西来支持一个观点,这有点乏味

public string StackingText
{
   get { return _Model.StackingText; }
}

是的,总是需要一遍又一遍地 。 (如果 C# 有一个自动实现视图模型 getter 和 setter 的语法结构,那就太好了。)但是一旦有了它,如何修改它以注入逻辑就很明显了:

public string StackingText
{
   get 
   { 
      return _Model.StackingRate == 0
         ? null
         : _Model.StackingText; 
   }
}

You can accomplish this in a trigger, as Meleak points out. If you were using MVVM, though, you wouldn't even have needed to ask this question.

The more I work with WPF, the clearer it is to me that everything's simpler if you just implement a view model. Yes, it's a little tedious to always have to write the likes of

public string StackingText
{
   get { return _Model.StackingText; }
}

over and over just to support a view. (It'd be nice if C# had a little syntax construct that implemented view model getters and setters automatically.) But once you have that, it's obvious how to modify it to inject logic:

public string StackingText
{
   get 
   { 
      return _Model.StackingRate == 0
         ? null
         : _Model.StackingText; 
   }
}
平安喜乐 2024-10-23 15:42:10

一个简单的可重用解决方案是编写一个转换器,将默认字符串作为参数。当您设置费率时,此转换器可以决定仅显示文本。

更新:

有关如何执行此操作的示例,请查看以下问题:
xaml 中的条件元素取决于绑定内容

An easy reusable solution would be to write a converter which takes your default string as a parameter. This converter can decide to only show the text, when your rate is set.

UPDATE:

For an example how to do this, look at this question:
Conditional element in xaml depending on the binding content

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文