Clear whitespace from end of string in WPF/XAML

2019-06-23 02:43发布

I have an MVVM application that uses a listbox which is populated with images. The image string always comes from an object that I can't modify because it's generated using an edmx model.

To cut a story shory, I need to put into the following xaml a way to trim the whitespace put onto the end of the image path by SQL from the string.

<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>

Is this possible?

2条回答
够拽才男人
2楼-- · 2019-06-23 02:50

Use a value converter in the binding which does the trimming for you.

查看更多
冷血范
3楼-- · 2019-06-23 02:57

If you do not want to use a converter your can do it right into your Property

INotifyChangedProperty Solution

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

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

DependencyProperty Solution

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()); }
    }
查看更多
登录 后发表回答