【问题标题】:XAML How do I create a TextBlock responsive?XAML 如何创建 TextBlock 响应式?
【发布时间】:2019-02-06 15:26:08
【问题描述】:

我有以下 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>

我想让TextBlock 显示所有发送的文本。例如,如果我调整大小(展开),它将仅显示第一次发送的文本(假设屏幕恢复向下)。谁能帮我解决这个问题?

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc xaml


    【解决方案1】:

    包含 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")); 
            }
        }
    }
    

    如果这是一个复杂的项目,我建议你使用像 MVVM-Light http://www.mvvmlight.net/ 这样的 MVVM 框架,我过去曾将它用于 Xamarin 开发。但是,还有许多其他选择。

    MVVM 的优点之一是您将 XAML(GUI) 与源代码完全解耦。我通常使用单独的项目来包含 MVVM 和 XAML。然后,您可以创建单元测试来练习 MVVM。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-10
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 2015-05-06
      相关资源
      最近更新 更多