繁体   English   中英

从WPF / XAML中的字符串末尾清除空格

[英]Clear whitespace from end of string in WPF/XAML

我有一个MVVM应用程序,它使用一个填充了图像的列表框。 图像字符串总是来自我无法修改的对象,因为它是使用edmx模型生成的。

为了剪切一个故事,我需要在下面的xaml中添加一种方法,通过SQL从字符串中修剪放在图像路径末尾的空白。

<ListBox ItemsSource="{Binding AllImages}" x:Name="listBox1" Width="300" Margin="10,10,0,10">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Grid.Column="0" Source="{Binding imagePath}" Height="100" Width="100" />
                <TextBlock Grid.Column="1" Text="{Binding imageId}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

这可能吗?

在绑定中使用值转换器为您进行修剪。

如果您不想使用转换器,您可以直接进入您的财产

INotifyChangedProperty解决方案

private string _ImageID;
public string ImageID
{
    get
    {
        return _ImageID;
    }

    set
    {
       value = (value == null ? value : value.Trim());
       NotifyPropertyChanged("ImageID");
    }
}

DependencyProperty解决方案

public static readonly DependencyProperty ImageIDProperty =
    DependencyProperty.Register("ImageID", typeof(string), typeof(MainWindowViewModel), new PropertyMetadata(string.Empty));

    public string ImageID
    {
        get { return (string)GetValue(ImageIDProperty); }
        set { SetValue(ImageIDProperty, value == null ? value : value.Trim()); }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM