views:

144

answers:

2

I thought it should be a simple thing for WPF, but I can't make it work... I have an int property (Divisions) on my class and I want to bind it to a DataGrid column.

<DataGrid AutoGenerateColumns="False">
    <DataGrid.Columns>
            <DataGridTextColumn Header="Number of Divisions" Binding="{Binding Path=Divisions, StringFormat={}\{0:N0\}}" IsReadOnly="True"/>                
    </DataGrid.Columns>
</DataGrid>

However, it doesn't show up. I also tried this code and it doesn't work for me either:

<DataGrid AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Divisions" IsReadOnly="True">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Divisions, StringFormat=C}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

For testing purpse, if I add a string property to this class to return string value of Divisions, it just works fine. So, what's wrong here?

+2  A: 

Interesting... I was able to get things to work easily using the following (in VS 2010, .NET 4) example.

My Grid Definition:

<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Number of Divisions" 
                            Binding="{Binding Path=Divisions,  StringFormat={}\{0:N0\}}" 
                            IsReadOnly="True"/>
    </DataGrid.Columns>
</DataGrid>

Setting the Data Context:

this.DataContext = new List<MyItem>() { 
    new MyItem() { Divisions = 1 },
    new MyItem() { Divisions = 2 },
    new MyItem() { Divisions = 3 },
    new MyItem() { Divisions = 4 },
    new MyItem() { Divisions = 5 }
};

My Data Class Definition:

public class MyItem {
    public int Divisions { get; set; } 
}

Hope this helps!

Pwninstein
Thank you very much for the quick response. I finally figured it out. The problem is not in StringFormat, but because the "Divisions" is defined as a public memeber variable, not as public property. This is one of the many members in this class and I have overlooked it. Actually, once this is changed to property, it works fine without StringFormat (for int). I'm sorry for confusion.
miliu
Not a problem, glad to help! Also, if this answer helped you fix your problem, make sure to click the check box to mark it as the accepted answer!
Pwninstein
A: 

I don't believe you're specifying your StringFormats correctly.

In the first example, try dropping the escape characters, like so: StringFormat={}{0:N0}

In the second: StringFormat={}{0:C}

djacobson
I tried his example both ways (with and without escaped braces) and numbers with thousand separators (i.e. commas) showed up correctly either way. Good thought though.
Pwninstein
I also tried several ways using StringFormat...
miliu
I also tried several ways using StringFormat...The following three return the same result: {Binding...StringFormat=N1}, {Binding...StringFormat=\{0:N1\}}, and {Binding...StringFormat={}\{0:N1\}}. Interestingly, in the second case, if I drop the escapte characters, compiler will complain. But in the third case, the first pair of curly braces should not be escaped, but the second pair has to.
miliu