如何在WPF

时间:2019-04-05 19:18:16

标签: wpf data-binding

我有一个对象集合,并且每个对象中都有一个属性列表(PropertyChanges)。该项目应列在“ listViewPropertyDetails” Listview下,但即使转换器“ propertyChangeConverter”也没有任何要转换的对象。对象中的任何其他属性都是可绑定的,例如用户名。但是,如果我尝试绑定集合“ PropertyChanges”,则不会显示任何内容,并且不会触发转换器。我忘记了什么?我正在寻找时间,却找不到正确的答案。希望有人能帮助我。

public class ObjectHistory
{
    public string Username { get; set; }
    public DateTime DateOccurred { get; set; }
    public List<PropertyChange> PropertyChanges = new List<PropertyChange>();
}

public class PropertyChange
{      
    public string PropertyName { get; set; }
    public string OldValue { get; set; }
    public string NewValue { get; set; }    
}
<DataTemplate x:Key="HistoryObjectList">
    <Expander IsExpanded="True">
        <Expander.Header>
            <MultiBinding Converter="{StaticResource historyObjectHeaderConverter}">
                <Binding Path="DateOccurred"/>
                <Binding Path="Username"/>
            </MultiBinding>
        </Expander.Header>
        <Grid Name="grid1"
              Height="Auto">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Label Name="LabelpropertyChanges"
                   Grid.Row="0"
                   HorizontalAlignment="Left"
                   Margin="15,25,0,0"
                   VerticalAlignment="Top"
                   Padding="0,0,0,0"
                   Content="PropertyChanges"
                   ></Label>                        
            <ListView Name="listViewPropertyDetails"
                      Grid.Row="1"
                      Margin="15,25,0,0"
                      HorizontalAlignment="Stretch"
                      HorizontalContentAlignment="Stretch"                                  
                      ItemsSource="{Binding PropertyChanges, Converter={StaticResource propertyChangeConverter}}"                                  
                      >                            
                <ListView.View>                            
                <GridView>
                        <GridViewColumn Header="PropertyName"

                                             Width="120"
                                             DisplayMemberBinding="{Binding PropertyName}"/>
                        <GridViewColumn Header="OldValue"
                                             Width="265"
                                             DisplayMemberBinding="{Binding OldValue}"/>
                        <GridViewColumn Header="NewValue"
                                             Width="265"
                                             DisplayMemberBinding="{Binding NewValue}"/>
                    </GridView>  
                </ListView.View>
            </ListView>
        </Grid>
    </Expander>
</DataTemplate>

1 个答案:

答案 0 :(得分:0)

PropertyChanges未被定义为属性,而是被定义为字段,并且您无法绑定到字段。您应该这样定义它:

public List<PropertyChange> PropertyChanges { get; set; } = new List<PropertyChange>();