将DataTable绑定到DataGrid:StringFormat不会更改视图

时间:2015-03-17 12:49:10

标签: c# wpf mvvm datagrid datatable

我将DataTable绑定到DataGrid,并在ItemsSource绑定中设置stringformat。但是,我的数据网格中的变量格式并不反映更改的字符串格式,并且没有给出错误。我正在使用GalaSoft的MVVM Light框架实现MVVM模式。

MainWindow中的数据网格如下所示:`

<Grid>
    <DataGrid Name="grid_Correlation"  
                            HorizontalAlignment="Left"
                            ItemsSource="{Binding correlationmatrix,StringFormat=p}"
                            CanUserAddRows="False" 
                            CanUserDeleteRows="False" CanUserReorderColumns="False" 
                            CanUserSortColumns="False" VerticalAlignment="Top" 
                            SelectionUnit="Cell" EnableColumnVirtualization="False" 
                            EnableRowVirtualization="False"
                            AutoGenerateColumns="True" >
    </DataGrid>
</Grid>

`

创建表的MainViewModel类等如下所示:

public class MainViewModel : ViewModelBase
{

    private DataTable _correlationmatrix;

    public DataTable correlationmatrix
    {
        get { return this._correlationmatrix; }
        set { this.Set("correlationmatrix", ref this._correlationmatrix, value); }
    }


    public MainViewModel()
    {

        _correlationmatrix = new DataTable();       

        for (int i = 0; i < 3; i++)
        {
            DataColumn col = new DataColumn();
            col.ColumnName = "Col " + i.ToString();
            col.DataType = typeof(Double);
            _correlationmatrix.Columns.Add(col);

        }

        for (int i = 0; i < 3; i++)
        {
            DataRow row = _correlationmatrix.NewRow();
            _correlationmatrix.Rows.Add(row);

        }

        for (int row = 0; row < 3; row++)
        {
            for (int col = 0; col < 3; col++)
            {
                _correlationmatrix.Rows[row][col] = 0.5;
            }
        }

    }
}

}

当我执行代码时,DataTable按预期绑定到数据网格。但是,没有任何值被格式化为百分比。

我错过了什么?

谢谢!

0 个答案:

没有答案