How Can I give white space to a string without giving actual space in XAML WPF?

MERUN KUMAR MAITY 596 Reputation points
2022-10-16T16:06:43.523+00:00

Hi there, I have a string name “Selected application” but I want to write it as “Selected application …” but I don’t want to do this in this way because it’s too much ugly I hope there some other methods are present. I know a method is present in C# called string.padright but I want to do this in WPF using pure XAML and data binding though efficient C# code is welcome.

But one problem is, this String "Selected application" is added from a List<>. I add this string in the last as a simple string. If I apply the string.padright method then my other items of the List is effected as well. I want an efficient way to adding space without adding space manually.

the code from which I add this "Selected application" in the list is :

devices.Insert(devices.Count - 0, new AudioDevice() { Name = "Selected application...", Direction = "Recording", Id = "Idlast", Default = false });  

As you see clearly, in the code Name = "Selected application..." I want to write it as Name = "Selected application ..." depending upon the space I want.

Please give your valuable opinion.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,774 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,925 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,531 Reputation points Microsoft Vendor
    2022-10-24T07:57:37.963+00:00

    You could try to set the data source string Selected application, set StringFormat when binding to add spaces and . . . .

    <Window.DataContext>  
            <local:ViewModel/>  
        </Window.DataContext>  
    
    <ComboBox Name="cb2" Width="400" Height="30" ItemsSource="{Binding List}"    
                     SelectedIndex="{Binding Selected}">  
                <ComboBox.ItemTemplate>  
                    <DataTemplate>  
                        <TextBlock Text="{Binding Name, StringFormat='{}{0}\  ...'}">  
                                 
                        </TextBlock>  
                    </DataTemplate>  
                </ComboBox.ItemTemplate>  
            </ComboBox>  
    

    Update:

        <Window.DataContext>  
                <local:ViewModel/>  
            </Window.DataContext>  
     <Window.Resources>  
            <local:Converter x:Key="converter"/>  
        </Window.Resources>  
        <ComboBox Name="cb2" Width="400" Height="30" ItemsSource="{Binding List}"    
                     SelectedIndex="{Binding Selected}"  DisplayMemberPath="Name" >  
                <ComboBox.ItemContainerStyle>  
                    <Style TargetType="{x:Type ComboBoxItem}">  
                        <Setter Property="ContentTemplate">  
                            <Setter.Value>  
                                <DataTemplate>  
                                    <StackPanel>  
                                        <TextBlock Text="{Binding Name}" />  
                                    </StackPanel>  
                                </DataTemplate>  
                            </Setter.Value>  
                        </Setter>  
                        <Style.Triggers>  
                            <DataTrigger Binding="{Binding Converter={StaticResource converter}, RelativeSource={RelativeSource Self}}" Value="IsLastItem">  
                                <Setter Property="ContentTemplate">  
                                    <Setter.Value>  
                                        <DataTemplate>  
                                            <StackPanel>  
                                                <TextBlock  Text="{Binding Name, StringFormat='{}{0}\  ...'}" />  
                                            </StackPanel>  
                                        </DataTemplate>  
                                    </Setter.Value>  
                                </Setter>  
                            </DataTrigger>  
                        </Style.Triggers>  
                    </Style>  
                </ComboBox.ItemContainerStyle>  
            </ComboBox>  
    

    Codebehind:

    253794-combobx-last-item-style-converter.txt

    Update2:

    The Code:
    256297-withespace.txt
    The result:

    256199-image.png

    ----------------------------------------------------------------------------

    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.