簡體   English   中英

綁定到DataGrid行工具提示WPF中的嵌套屬性

[英]Binding to a nested Property in DataGrid row tooltip WPF

我不能讓這個工作。 我有一個視圖),其中包含一個DataGrid,其中填充了一個可觀察集合(MyDataCollection)的項目。 MyDataCollection的每個項目都有不同的屬性(Name,Description,...,Logs)。 Logs是Log項目的可觀察集合。 每個Log項都有不同的屬性(Date,Person,...)。

填充了MyDataCollection項的數據網格每行設置一個工具提示。 像這樣:

<DataGrid ItemsSource="{Binding MyDataCollection}">
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <Border>
                                <Grid Margin="5" MaxWidth="400">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*" />
                                        ...
                                    </Grid.RowDefinitions>

                                    ...
                                    <DataGrid x:Name="LogsGrid" Grid.Row="6" ItemsSource="{Binding PlacementTarget.DataContext.Logs, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ToolTip}}">
                                        <DataGrid.Columns>
                                            <DataGridTextColumn Header="Date" 
                                                Binding="{Binding Date}" 
                                                />
                                            <DataGridTextColumn Header="Person" 
                                                Binding="{Binding Person.FullName}" 
                                                />

                                        </DataGrid.Columns>
                                    </DataGrid>
                                </Grid>
                            </Border>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>

我可以看到工具提示,我可以在工具提示中看到帶有標題“日期”和“人物”的數據網格,但網格內容為空。 看起來綁定設置不正確。 任何人都可以幫我一把嗎? 謝謝

更新1: MyDataColletion包含我的自定義類“Car”的對象。 這里定義了Car:

public class Car : INotifyPropertyChanged
{
    public string name;
    public string description;
    public Contact assignedTo;
    public ObservableCollection<Log> logs = new ObservableCollection<Log>();
    public string Name
    {
        get
        {
            return this.name;
        }
        set
        {
            if (this.name != value)
            {
                this.name = value;
                NotifyPropertyChanged("Name");
            }
        }
    }
    public string Description 
    {
        get
        {
            return this.description;
        }
        set
        {
            if (this.description != value)
            {
                this.description = value;
                NotifyPropertyChanged("Description");
            }
        }
    }
    public Contact AssignedTo 
    {
        get
        {
            return this.assignedTo;
        }
        set
        {
            if (this.assignedTo != value)
            {
                this.assignedTo = value;
                NotifyPropertyChanged("AssignedTo");
            }
        }
    }

    public ObservableCollection<Log> Logs
    {
        get
        {
            return this.logs;
        }
        private set //TODO : Check if this is correct
        {
            if (this.logs != value)
            {
                this.logs = value;
                NotifyPropertyChanged("Logs");
            }
        }
    }

    public Car()
    {
        // TODO: Delete this: (only here for testing)
        Contact c = new Contact();
        c.Name = "Test";
        c.LastName = "Test";
        for (int i = 0; i < 4; i++)
            AddLog(DateTime.Now, c, new TimeSpan(2, 0, 0));
    }
    public void AddLog(DateTime date, Contact person, TimeSpan time)
    {
        Log newLog = new Log(date, person, time);
        Logs.Add(newLog);
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

我的日志類是:

public class Log : INotifyPropertyChanged
{
    DateTime date; 
    Contact person;
    TimeSpan time;
    public DateTime Date
    {
        get
        {
            return this.date;
        }
        set
        {
            if (this.date != value)
            {
                this.date = value;
                NotifyPropertyChanged("Date");
            }
        }
    }
    public Contact Person
    {
        get
        {
            return this.person;
        }
        set
        {
            if (this.person != value)
            {
                this.person = value;
                NotifyPropertyChanged("Person");
            }
        }
    }
    public TimeSpan Time
    {
        get
        {
            return this.time;
        }
        set
        {
            if (this.time != value)
            {
                this.time = value;
                NotifyPropertyChanged("Time");
            }
        }
    }

    public Log(DateTime date, Contact person, TimeSpan time)
    {
        this.date = date;
        this.person = person;
        this.time = time;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }
}

而不是ItemsSource="{Binding Logs, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> ,使用: ItemsSource="{Binding PlacementTarget.DataContext.Logs, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ToolTip}}">

我在代碼中找到的唯一不能完美運行的東西是LogsGrid.ItemsSource綁定的Mode=TwoWay 這引發了我的異常,因為它告訴Binding你希望LogsGridItemsSource新值寫回Binding源 - 在本例中,是viewmodel的Logs屬性。 這不僅不是你想要的,而且它實際上是不可能的,因為Logs有一個私有的setter(而且, DataGrid無論如何都不會這樣做)。 因此例外。

UpdateSourceTrigger=PropertyChanged是另一個沒有用處的,雖然這次沒有任何意義:它告訴它何時將新的Logs集合寫回Car.Logs 但同樣, DataGrid無法做到這一點。 它不會為屬性創建新值。 TextBox會將新的Text值分配給source屬性,這就是TextBox的用途。 DataGrid不這樣做。

當我擺脫這兩件事時,它對我來說很好:

<DataGrid x:Name="LogsGrid" Grid.Row="6" ItemsSource="{Binding Logs}">
    <!-- etc. -->

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM