简体   繁体   中英

WPF DataGridCell Style binding issue

I'm facing a strange issue binding Collection to DataGrid on which when selecting a row the binding seems to disappear (I have an empty cell)

Here is my object list

public ReadOnlyCollection<ItemPoint> CurrentItemPoints { get; private set; }

ItemPoint object is defined as :

public sealed class ItemPoint : PropertyChangedNotifierBase
{        
    private bool _IsSubscribed = false;
    private string _Name = string.Empty;
    private string _Value = string.Empty;
    private DateTime _ValueTime = DateTime.UtcNow;

    public bool IsSubscribed
    {
        get { return _IsSubscribed; }
        set
        {
            if (_IsSubscribed != value)
                ChangeSubscription(value);
            ChangeProperty("IsSubscribed", ref _IsSubscribed, value);
        }
    }

    public string Name
    {
        get { return _Name; }
        private set
        {
            ChangeProperty("Name", ref _Name, value);
        }
    }

    public string Value
    {
        get { return _Value; }
        set
        {
            _Value = value;
        }
    }

    public DateTime ValueTime
    {
        get { return _ValueTime; }
        private set
        {
            ChangeProperty("ValueTime", ref _ValueTime, value);
        }
    }
}

here is my xaml:

    <DataGrid Name="PointsList" VerticalAlignment="Stretch"  HorizontalAlignment="Stretch" Margin="2,0,0,0"
              ItemsSource="{Binding CurrentItemPoints, UpdateSourceTrigger=PropertyChanged}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn Binding="{Binding Path=IsSubscribed, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"  Width="10">
                <DataGridCheckBoxColumn.CellStyle>
                    <Style>
                        <EventSetter Event="CheckBox.Checked" Handler="SubscriptionCheckBox_Checked"/>
                    </Style>
                </DataGridCheckBoxColumn.CellStyle>
            </DataGridCheckBoxColumn>
            <DataGridTextColumn Binding="{Binding Name, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}" Header="Name"  Width="*"/>
            <DataGridTextColumn Binding="{Binding Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, IsAsync=True, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}"  Header="WorkingValueColumn" Width="*"/>
            <DataGridTextColumn  Binding="{Binding Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, IsAsync=True, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}"  Header="NonWorkingValueColumn" Width="*">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type DataGridCell}">
                                    <ContentPresenter/>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Path=ValueTime, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, Mode=OneWay, StringFormat='{}{0:dd-MM-yyyy HH:mm:ss.fff}'}" Header="Time"  Width="*">
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

If I now display the datagrid everything is ok... until I select any cell of the grid.

Doing that I will have the "WorkingValueColumn" working well... meaning that will keep displaying the selected item value while the "NonWorkingValueColumn" will display an empty cell...

Here is a screenshot of part of datagrid as sample: 带有选择单元格的Datagrid

I've been looking for a while now without finding any kind of explication... I though I could have missed some selection template or something like that but I really don't know.

Thanks for your help!

I finally could workaround that strange behavior changing the DataTextColumn by a DataTemplateColumn as that:

<DataGridTemplateColumn Header="Value">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                       <TextBlock Text="{Binding Value, UpdateSourceTrigger=PropertyChanged, Mode=OneWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" HorizontalAlignment="Stretch"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" BorderThickness="0" HorizontalAlignment="Stretch"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM