簡體   English   中英

WPF DataGrid從DataGridCell樣式綁定到當前對象屬性

[英]WPF DataGrid binding to current object property from DataGridCell style

我下面的示例工作正常,當Id.Updated屬性為true時,它將突出顯示Id列中的單元格。

我想知道如何修改綁定表達式Binding="{Binding Id.Updated}" ,以便在適當的列(不僅是Id)中綁定到當前IssueElement對象的Updated屬性。

我希望能夠對所有列僅使用一種樣式,而不對每列使用一種樣式。

下面的示例是DataGrid在我的應用程序中的工作方式的簡化版本。

數據網格:

<DataGrid ItemsSource="{Binding IssueList}" AutoGenerateColumns="False" >
    <DataGrid.Resources>
        <Style x:Key="TestStyle" TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Id.Updated}" Value="True">
                    <Setter Property="Background" Value="Green" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.Resources>   
    <DataGrid.Columns>
        <DataGridTextColumn Header="Id" Binding="{Binding Id.Value}" CellStyle="{StaticResource TestStyle}" />
        <DataGridTextColumn Header="Title" Binding="{Binding Title.Value}" CellStyle="{StaticResource TestStyle}" />
        <DataGridTextColumn Header="Body" Binding="{Binding Body.Value}" CellStyle="{StaticResource TestStyle}" />
    </DataGrid.Columns>
</DataGrid>

集合:

private ObservableCollection<Issue> mIssueList;
public ObservableCollection<Issue> IssueList
{
    get { return mIssueList; }
    set { mIssueList = value; OnPropertyChanged("IssueList"); }
}

集合使用的類

public class Issue
{
    public IssueElement Id { get; set; }
    public IssueElement Title { get; set; }
    public IssueElement Body { get; set; }
}

public class IssueElement
{
    public string Value { get; set; }
    public bool Updated { get; set; }
}

提前致謝

我不確定為什么您的綁定中包含One.Value,Two.Value,Three.Value。 我認為您的意思是Id.Value,Title.Value和Body.Value,對嗎?

我認為唯一的方法就是使用轉換器。 這是一種實現方法:

<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource UpdatedConverter}}" Value="True">
    <Setter Property="Background" Value="Green" />
</DataTrigger>

和轉換器:

public class UpdatedConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        DataGridCell dgc = value as DataGridCell;
        if (dgc != null)
        {
            Issue data = dgc.DataContext as Issue;
            if (data != null)
            {
                DataGridTextColumn t = dgc.Column as DataGridTextColumn;
                if (t != null)
                {
                    var binding = t.Binding as System.Windows.Data.Binding;
                    if (binding != null && binding.Path != null && binding.Path.Path != null)
                    {
                        string val = binding.Path.Path.ToLower();
                        if (val.StartsWith("id"))
                            return data.Id.Updated;
                        if (val.StartsWith("title"))
                            return data.Title.Updated;
                        if (val.StartsWith("body"))
                            return data.Body.Updated;
                    }
                }
            }
        }
        return false;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

暫無
暫無

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

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