簡體   English   中英

如何為DataGrid優化代碼

[英]How to optimize code for datagrid

我想在我的應用程序中重復這種設計。

http://imageshack.us/photo/my-images/651/50171626.jpg/

我寫了這段代碼。

public class CellViewModel
{
    public string Background { get; set; }

    public string FontWeight { get; set; }

    public string Foreground { get; set; }

    public string Value { get; set; }
}

DataGridCell的ViewModel


public class PerformanceItemViewModel
{
    public CellViewModel Title { get; set; }

    public CellViewModel AllTrades { get; set; }

    public CellViewModel LongTrades { get; set; }

    public CellViewModel ShortTrades { get; set; }

    public CellViewModel BuyAndHold { get; set; }
}

public class PerformanceViewModel
{
    public PerformanceViewModel()
    {
        this.Items = new ObservableCollection<PerformanceItemViewModel>
            {
                new PerformanceItemViewModel
                    {
                        Title = new CellViewModel { Value = "Net Profit", FontWeight = "Bold", Foreground = "Black", Background = "White" },
                        AllTrades = new CellViewModel { Value = "-683,84", FontWeight = "Normal", Foreground = "Red", Background = "White" },
                        LongTrades = new CellViewModel { Value = "-683,84", FontWeight = "Normal", Foreground = "Red", Background = "White" },
                        ShortTrades = new CellViewModel { Value = "0,00", FontWeight = "Normal", Foreground = "Black", Background = "White" },
                        BuyAndHold = new CellViewModel { Value = "-2010,00", FontWeight = "Normal", Foreground = "Red", Background = "White" }
                    }
            };
    }

    public ObservableCollection<PerformanceItemViewModel> Items { get; set; }
}

我想優化此代碼。

主要問題是如何通知DataGrid有關Background,FontWeight,Foreground的信息?


<Grid>
    <datagrid:ThemedDataGrid AlternatingRowBackground="{Binding Background}"
                             AutoGenerateColumns="False"
                             GridLinesVisibility="None"
                             HeadersVisibility="Column"
                             IsHitTestVisible="False"
                             IsReadOnly="True"
                             ItemsSource="{Binding Performance.Items}">

        <datagrid:ThemedDataGrid.CellStyle>
            <Style TargetType="DataGridCell">
                <Setter Property="BorderThickness" Value="0" />
            </Style>
        </datagrid:ThemedDataGrid.CellStyle>

        <datagrid:ThemedDataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Path=Title.Value}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Background" Value="{Binding Path=Title.Background}" />
                        <Setter Property="FontWeight" Value="{Binding Path=Title.FontWeight}" />
                        <Setter Property="Foreground" Value="{Binding Path=Title.Foreground}" />
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Header="All Trades" Binding="{Binding Path=AllTrades.Value}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Background" Value="{Binding Path=AllTrades.Background}" />
                        <Setter Property="FontWeight" Value="{Binding Path=AllTrades.FontWeight}" />
                        <Setter Property="Foreground" Value="{Binding Path=AllTrades.Foreground}" />
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Long Trades" Binding="{Binding Path=LongTrades.Value}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Background" Value="{Binding Path=LongTrades.Background}" />
                        <Setter Property="FontWeight" Value="{Binding Path=LongTrades.FontWeight}" />
                        <Setter Property="Foreground" Value="{Binding Path=LongTrades.Foreground}" />
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Short Trades" Binding="{Binding Path=ShortTrades.Value}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Background" Value="{Binding Path=ShortTrades.Background}" />
                        <Setter Property="FontWeight" Value="{Binding Path=ShortTrades.FontWeight}" />
                        <Setter Property="Foreground" Value="{Binding Path=ShortTrades.Foreground}" />
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Buy And Hold" Binding="{Binding Path=BuyAndHold.Value}">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="Background" Value="{Binding Path=BuyAndHold.Background}" />
                        <Setter Property="FontWeight" Value="{Binding Path=BuyAndHold.FontWeight}" />
                        <Setter Property="Foreground" Value="{Binding Path=BuyAndHold.Foreground}" />
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>
        </datagrid:ThemedDataGrid.Columns>
    </datagrid:ThemedDataGrid>
</Grid>

但是不幸的是,這段代碼非常麻煩。

也許您有一些改進的想法?

WPF中的DataGrid支持分組,請看一下本文如何:在DataGrid控件中對數據進行分組,排序和篩選 MSDN上同樣是非常相似您的問題。

暫無
暫無

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

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