stackpanel 中的 wpf 文本块自动调整大小/布局

发布于 2024-08-13 04:19:22 字数 652 浏览 6 评论 0原文

我有以下堆栈面板

<StackPanel>
                <TextBlock Text="{Binding AddressLine1}" />
                <TextBlock Text="{Binding AddressLine2}" />
                <TextBlock Text="{Binding AddressLine3}"  />
                <TextBlock Text="{Binding AddressLine4}"  />
</StackPanel>

,并且绑定对象上的 AddressLine2 字符串为空。

我的堆栈面板呈现为类似的效果

| AddressLine1 |
|              |
| AddressLine3 |
| AddressLine4 |

,但我希望它呈现为类似的效果,

| AddressLine1 |
| AddressLine3 |
| AddressLine4 |

这可能吗,还是我缺少一种明显的方法来做到这一点?

谢谢, 克里斯

I have the following stack panel

<StackPanel>
                <TextBlock Text="{Binding AddressLine1}" />
                <TextBlock Text="{Binding AddressLine2}" />
                <TextBlock Text="{Binding AddressLine3}"  />
                <TextBlock Text="{Binding AddressLine4}"  />
</StackPanel>

and my AddressLine2 string is null on the bound object.

My stack panel renders like

| AddressLine1 |
|              |
| AddressLine3 |
| AddressLine4 |

but I want it to render like

| AddressLine1 |
| AddressLine3 |
| AddressLine4 |

is this possible, or am I missing an obvious way to do it?

Thanks,
Chris

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

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

发布评论

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

评论(3

后eg是否自 2024-08-20 04:19:22

创建一个实现 IMultiValueConverter 的转换器,然后在文本上使用 MultiBinding,这样每一行只有一个 TextBlock,如下所示:

class MultiStringConverter : IMultiValueConverter
{
    public object Convert( object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture )
    {
        var text = new StringBuilder( );
        for ( int i = 0 ; i < values.Length ; i++ ) {
            string line = String.Format( "{0}", values[i] );
            if ( !String.IsNullOrEmpty( line ) ) {
                text.AppendLine( line );
            }   // if
        }
        return text.ToString( );
    }

    public object[] ConvertBack( object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture )
    {
        throw new NotImplementedException( );
    }
}

在 XAML 中...

    <TextBlock>
        <TextBlock.Text>
            <MultiBinding>
                <MultiBinding.Converter>
                    <Local:MultiStringConverter />
                </MultiBinding.Converter>
                <Binding Path="AddressLine1" />
                <Binding Path="AddressLine2" />
                <Binding Path="AddressLine3" />
                <Binding Path="AddressLine4" />
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>

Create a converter that implements IMultiValueConverter then use a MultiBinding on the text so that you only have one TextBlock with each line like this:

class MultiStringConverter : IMultiValueConverter
{
    public object Convert( object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture )
    {
        var text = new StringBuilder( );
        for ( int i = 0 ; i < values.Length ; i++ ) {
            string line = String.Format( "{0}", values[i] );
            if ( !String.IsNullOrEmpty( line ) ) {
                text.AppendLine( line );
            }   // if
        }
        return text.ToString( );
    }

    public object[] ConvertBack( object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture )
    {
        throw new NotImplementedException( );
    }
}

And in the XAML...

    <TextBlock>
        <TextBlock.Text>
            <MultiBinding>
                <MultiBinding.Converter>
                    <Local:MultiStringConverter />
                </MultiBinding.Converter>
                <Binding Path="AddressLine1" />
                <Binding Path="AddressLine2" />
                <Binding Path="AddressLine3" />
                <Binding Path="AddressLine4" />
            </MultiBinding>
        </TextBlock.Text>
    </TextBlock>
池予 2024-08-20 04:19:22

您可以使用 ValueConverter 将 TextBlock 的 Visibility 绑定到 Text 属性

You can bind the Visibility of the TextBlock to the Text property by using a ValueConverter

孤凫 2024-08-20 04:19:22

您可以使用 TextBlock 的触发器来检查 Text 是否为空,并在这种情况下将 Visibility 设置为 Collapsed。

you can use Trigger for TextBlock for checking if Text is null and set the Visibility to Collapsed in that case.

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