【问题标题】:Texblock text decoration not getting updated UWP文本块文本装饰未更新 UWP
【发布时间】:2018-08-16 02:59:24
【问题描述】:

我有一个简单的应用程序,它应该更改文本装饰并为列表视图中的每个项目启用/禁用两个按钮。它适用于两个按钮,但由于某种原因不适用于文本装饰。我不确定我在这里可能缺少什么。任何帮助将不胜感激

型号:

public class TaskModel : ViewModelBase
{
    private string _id;
    private string _status;

    public string ID
    {
        get => _id;
        set
        {
            _id = value;
            RaisePropertyChanged(nameof(ID));
            RaisePropertyChanged(nameof(IsNew));
        }
    }
    public string Text { get; set; }
    public bool CanBeMarkedAsCompleted
    {
        get
        {
            if (!IsNew && Status != "completed")
                return true;
            else
                return false;
        }
    }

    public bool CanBeMarkedAsIncompleted
    {
        get
        {
            if (!IsNew && Status == "completed")
                return true;
            else
                return false;
        }
    }

    public string Status
    {
        get => _status;
        set
        {
            _status = value;
            RaisePropertyChanged(nameof(Status));
            RaisePropertyChanged(nameof(CanBeMarkedAsCompleted));
            RaisePropertyChanged(nameof(CanBeMarkedAsIncompleted));
        }
    }

    public bool IsNew
    {
        get => string.IsNullOrEmpty(ID);
    }
}

观点:

      <converters:BoolToTextDecorationConverter
        x:Key="BoolToTextDecorationConverter"/>
     <ScrollViewer Grid.Row="1">
        <StackPanel>
            <TextBox
                HorizontalAlignment="Center"
                FontSize="20"
                Text="The item text decoration should change" />
            <Button Name="ChangeTextDecoration_Button" Click="ChangeTextDecoration_Button_Click">Change Text Decoration Status</Button>

            <ListView Name="TaskListView" ScrollViewer.VerticalScrollBarVisibility="Visible">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="VerticalAlignment" Value="Center" />
                        <Setter Property="HorizontalAlignment" Value="Stretch" />
                        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                    </Style>
                </ListView.ItemContainerStyle>
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="lm:TaskModel">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <TextBlock
                                Grid.Column="0"
                                Text="{Binding Text, Mode=OneWay}"
                                TextDecorations="{Binding CanBeMarkedAsCompleted, Mode=OneWay, Converter={StaticResource BoolToTextDecorationConverter}}" />
                            <Button Grid.Column="1" IsEnabled="{Binding CanBeMarkedAsCompleted, Mode=OneWay}">CanBeMarkedAsCompleted_Button</Button>
                            <Button Grid.Column="2" IsEnabled="{Binding CanBeMarkedAsIncompleted, Mode=OneWay}">CanBeMarkedAsIncompleted_Button</Button>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackPanel>
    </ScrollViewer>

代码隐藏:

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {

        TaskListView.ItemsSource = new List<TaskModel>
        {
            new TaskModel
            {
                ID = "1",
                Status = "completed",
                Text = "Note 1"
            },
            new TaskModel
            {
                ID = "2",
                Status = "needsAction",
                Text = "Note 2"
            },
            new TaskModel
            {
                ID = "3",
                Status = "completed",
                Text = "Note 3"
            },
            new TaskModel
            {
                ID = "4",
                Status = "needsAction",
                Text = "Note 4"
            },
        };
    }

    private void ChangeTextDecoration_Button_Click(object sender, RoutedEventArgs e)
    {
        var tasks = (List<TaskModel>)TaskListView.ItemsSource;

        if (_firstClick)
        {
            foreach (var item in tasks)
            {
                item.Status = "needsAction";
            }
        }
        else
        {

            foreach (var item in tasks)
            {
                item.Status = "completed";
            }
        }


        _firstClick = !_firstClick;
    }

转换器:

public class BoolToTextDecorationConverter : IValueConverter
{
    public TextDecorations OnTrue { get; set; }
    public TextDecorations OnFalse { get; set; }

    public BoolToTextDecorationConverter()
    {
        OnTrue = TextDecorations.None;
        OnFalse = TextDecorations.Strikethrough;
    }

    public object Convert(object value, Type targetType, object parameter, string language)
    {
        bool v = (bool)value;
        return v ? OnTrue : OnFalse;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

【问题讨论】:

  • 你确定你在Page.ResourcesApplication.Resources上为转换器定义了StaticResource
  • @MartinZikmund 是的,我愿意
  • 在这种情况下,您可以尝试在bool v = 赋值上放置一个断点,看看Convert 方法是否被命中?
  • 是的,它被击中了 x.x

标签: c# .net xaml uwp uwp-xaml


【解决方案1】:

@Efrain Bastidas Berrios 感谢您的反馈。这是一个已知问题。相关团队一直在调查此问题。

您也可以通过在将 TextBlock.TextDecorations 设置为 None 后修改 TextBlock.Text 属性来解决此问题。

【讨论】:

  • 这解释了一切......谢谢你的回答
猜你喜欢
  • 2019-12-20
  • 2020-03-19
  • 1970-01-01
  • 2012-06-10
  • 1970-01-01
  • 1970-01-01
  • 2011-08-09
  • 2011-07-26
  • 2015-03-03
相关资源
最近更新 更多