简体   繁体   English

XAML如何创建一个TextBlock响应式?

[英]XAML How do I create a TextBlock responsive?

I have the following XAML code : 我有以下XAML代码:

<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. 我想让TextBlock显示所有发送的文本。 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: 包含Title的类必须实现INotifyPropertyChanged您的类需要看起来像这样:

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. 如果这是一个复杂的项目,我建议您使用MVVM-Light http://www.mvvmlight.net/之类的MVVM框架,我过去曾将其用于Xamarin开发。 However, there are many other alternatives. 但是,还有许多其他选择。

One of the advantages of MVVM, is you completely decouple the XAML(GUI) from the source. MVVM的优点之一是,您可以将XAML(GUI)与源代码完全分离。 I normally use separate projects to contain the MVVM and XAML. 我通常使用单独的项目来包含MVVM和XAML。 You can then create unit tests to exercise the MVVM. 然后,您可以创建单元测试以练习MVVM。

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

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