文本块文本装饰未更新 UWP

Texblock text decoration not getting updated UWP

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

型号:

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();
    }
}

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

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