简体   繁体   中英

XAML How do I create a TextBlock responsive?

I have the following XAML code :

<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid Background="Transparent" Loaded="OnGridLoaded">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="5" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBlock Foreground="{StaticResource CaptionColorBrush}" Text="&gt;" />
            <TextBlock Grid.Column="1" Background="Transparent" Style="{StaticResource BreadCrumsTextBlock}" 
                FontWeight="{Binding IsLastElement, Converter={StaticResource LastElementToFontWeightConverter}}"
                Text="{Binding Title}" Margin="5 0 0 0" TextTrimming="CharacterEllipsis" />
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="PreviewMouseLeftButtonDown">
                    <command:EventToCommand Command="{Binding Mode=OneWay, Path=DataContext.ReturnToFolderCommand, ElementName=ChannelsTab}" CommandParameter="{Binding NodeId}" />
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>

I want to make TextBlock to show all text send. For example if I resize (expand) it will display only the text is send for the first time (suppose the screen is restored down). Can anyone help me to fix the problem?

The class that contains Title must implement INotifyPropertyChanged Your class needs to look something like this:

public class Model : INotifyPropertyChanged
{
    //This is all that the interface requires
    public event PropertyChangedEventHandler PropertyChanged;

    private string _title;
    public string Title
    {
        get { return _title; }
        set
        {
            _text = value;
            if(PropertyChanged != null)
                PropertyChange(this, new PropertyChangedEventArgs("Title")); 
        }
    }
}

If this is a complex project, I suggest you use an MVVM Framework like MVVM-Light http://www.mvvmlight.net/ I have used it in the past for Xamarin development. However, there are many other alternatives.

One of the advantages of MVVM, is you completely decouple the XAML(GUI) from the source. I normally use separate projects to contain the MVVM and XAML. You can then create unit tests to exercise the MVVM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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