Texblock文本修饰未更新UWP

时间:2018-08-16 02:59:25

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

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

型号:

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

1 个答案:

答案 0 :(得分:1)

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

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