【问题标题】:UWP Bind Listview items foreground to propertyUWP 将 Listview 项目前景绑定到属性
【发布时间】:2018-02-08 13:22:01
【问题描述】:

我想根据这些项目的属性更改 ListView 中某些项目的前景。如果项目的“EsBlacklist”属性设置为 true,则其前景应为红色。

<Page.Resources>
    <converter:ForegroundColorConverter x:Key="ForegroundConverter" x:Name="ForegroundConverter"/>
</Page.Resources>

<StackPanel Grid.Column="1" Grid.Row="1">
            <TextBlock HorizontalAlignment="Center" Margin="10" FontSize="24">Vehículos sin VTV</TextBlock>
            <ListView ItemsSource="{x:Bind ViewModel.PatentesSinVtv}" Margin="10" DisplayMemberPath="Placa" 
                      SelectedItem="{x:Bind ViewModel.PatenteSeleccionada, Mode=TwoWay}"
            HorizontalAlignment="Center"
            IsItemClickEnabled="False"
            IsSwipeEnabled="False"
            CanDragItems="False"
            SelectionMode="Single"
            Grid.Column="1"
            Grid.Row="1">
                <ListViewItem Foreground="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource ForegroundConverter}}"></ListViewItem> 
( Self should reference the item and not ListViewItem.)
            </ListView>    
</StackPanel>

还有转换器:

class ForegroundColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            var patente = (Patente)value; //value is not a Patente but ListViewItem
            return patente.EsBlacklist ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Gray);
        }
    }

我的问题是转换器中收到的“价值”不是Patente,而是ListViewItem

【问题讨论】:

    标签: c# data-binding uwp


    【解决方案1】:

    我的问题是转换器中收到的“价值”不是Patente,而是ListViewItem

    作为{RelativeSource} markup extension的文档,

    {RelativeSource Self} 产生 Self 的 Mode 值。目标元素应用作此绑定的源。这对于将元素的一个属性绑定到同一元素的另一个属性很有用。 ...

    Self 模式可用于将元素的一个属性绑定到同一元素上的另一个属性,它是 ElementName 绑定的一种变体,但不需要命名然后自引用元素。

    这里是一个使用RelativeSource={RelativeSource Self}的例子,

    <Rectangle
      Fill="Orange" Width="200"
      Height="{Binding RelativeSource={RelativeSource Self}, Path=Width}"/>
    

    可以看文档了解如何使用{RelativeSource} markup extension

    您可以直接绑定ViewModel,使转换器成为Patente,

    <ListViewItem Foreground="{x:Bind ViewModel, Mode=TwoWay, Converter={StaticResource ForegroundConverter}}"/>
    

    【讨论】:

    • 但是我不会通过绑定整个 ViewModel 去任何地方,因为我需要准确地绑定每一行中的项目,因为行颜色取决于项目属性。
    • @Vallo 我不太清楚您的想法,但是对于您的这个问题,您的问题是转换器中收到的“价值”不是专利而是 ListViewItem?你想要的真实效果是什么?你的 ViewModel 和 Model 是什么样子的?当您绑定 ViewModel 并将模型项的属性通过双向绑定绑定到前台时,通常也像您所做的那样使用转换,它应该能够实现效果。
    • 抱歉耽搁了。 ViewModel.PatentesSinVtv 是 Patente 的 ObservableCollection。这绑定到 ListView 的 ItemsSource。我想要实现的是通过转换器将每一行的前景更改为EsBlackList 属性。如果EsBlackList 为真,则该行的前景应为红色。
    【解决方案2】:

    前景色的取值不是素色,而是画笔。 所以你的转换器应该返回new SolidColorBrush(Colors.Red)

    【讨论】:

    • 好的,但我的主要问题是我在转换器中收到的对象“值”不是Patente,而是ListViewItem。为了清楚起见,我正在编辑问题。
    • 好吧,误会你了。。现在无法测试,但可能 (Patente)((value as ListViewItem).DataContext) 会起作用。
    【解决方案3】:

    你可以这样处理:

    <ListViewItem>
        <ListViewItem.Foreground>
            <SolidColorBrush Color="{x:Bind YourColor}"/>
        </ListViewItem.Foreground>
    </ListViewItem>
    

    【讨论】:

    • 我认为你应该将绑定设置为单向
    • 如果你想在运行时改变它,你可以
    • 我认为如果 Vallo 想一次性绑定一个值,我建议他可以将其写为资源。
    【解决方案4】:

    我需要实现一个 ItemTemplate

    <ListView.ItemTemplate>
        <DataTemplate x:DataType="modelo:Patente">
            <TextBlock Text="{Binding Placa}" Foreground="{x:Bind EsBlacklist, Mode=TwoWay, Converter={StaticResource ForegroundConverter}}"></TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
    

    转换器变成这样:

    public object Convert(object value, Type targetType, object parameter, string language)
        {
            return (bool)value? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Gray);
        }
    

    【讨论】:

      猜你喜欢
      • 2019-09-29
      • 1970-01-01
      • 1970-01-01
      • 2016-11-30
      • 2020-08-02
      • 2018-05-25
      • 1970-01-01
      • 2017-11-08
      • 2018-09-03
      相关资源
      最近更新 更多