Click here to Skip to main content
15,910,980 members
Home / Discussions / WPF
   

WPF

 
QuestionDataGridCell Style Problem Pin
Kevin Marois25-Oct-21 11:20
professionalKevin Marois25-Oct-21 11:20 
I'm creating a WPF DataGrid. The data source is a DataTable created in the VM. See this pic.

I'm creating a DataTable in the VM, then binding the DataGrid to it. That part all works fine. I now want to apply a cell style:

View Model
string[] columnNames = (from dc in BudgetTable.Columns.Cast<DataColumn>()
                        select dc.ColumnName).ToArray();

// Go through each column
foreach (string item in columnNames)
{
    //Creating binding
    Binding binding = new Binding(item);
    string styleName = "";

    if (item == "Type")
    {
        binding.Mode = BindingMode.OneTime;
        styleName = "budgetGridCellStyle";
    }
    else
    {
        binding.Mode = BindingMode.TwoWay;
        binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
        styleName = "dataGridCurrencyCellStyle";
    }

    var style = Application.Current.FindResource(styleName) as Style;

    // Bind the column
    var col = new DataGridTextColumn()
    {
        Header = item,
        Binding = binding,
        Visibility = Visibility.Visible,
        CellStyle = style
    };

    DataGridColumns.Add(col);
}

Style
<Style x:Key="dataGridCurrencyCellStyle" 
        BasedOn="{StaticResource dataGridCellStyle}"
        TargetType="{x:Type DataGridCell}">

    <Setter Property="TextBlock.Text" Value="{Binding StringFormat={}{0:0.##}}"/>
    <Setter Property="IsEnabled" Value="{Binding AreFieldsEnabled}"/>

</Style>
The setters won't work:
  1. The string format setting is not being applied no matter what I change it to. What's the right way to do this?
  2. The IsEnabled causes a BindingExpression error. AreFieldsEnabled is a bool property on the VM. How do I reference that from this style?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.


modified 27-Oct-21 16:36pm.

QuestionWPF MVVM Modeless Dialogs - any Example? Pin
Amarnath S23-Oct-21 21:08
professionalAmarnath S23-Oct-21 21:08 
AnswerRe: WPF MVVM Modeless Dialogs - any Example? Pin
Gerry Schmitz24-Oct-21 9:22
mveGerry Schmitz24-Oct-21 9:22 
GeneralRe: WPF MVVM Modeless Dialogs - any Example? Pin
#realJSOP9-Nov-21 23:24
professional#realJSOP9-Nov-21 23:24 
AnswerRe: WPF MVVM Modeless Dialogs - any Example? Pin
#realJSOP9-Nov-21 23:30
professional#realJSOP9-Nov-21 23:30 
QuestionHow do I create xy plots with WPF APP (.NET Framework)? --- Solved Pin
Member 1539432815-Oct-21 5:12
Member 1539432815-Oct-21 5:12 
AnswerRe: How do I create xy plots with WPF APP (.NET Framework)? Pin
Richard Deeming15-Oct-21 6:05
mveRichard Deeming15-Oct-21 6:05 
AnswerRe: How do I create xy plots with WPF APP (.NET Framework)? Pin
Gerry Schmitz15-Oct-21 9:23
mveGerry Schmitz15-Oct-21 9:23 
GeneralRe: How do I create xy plots with WPF APP (.NET Framework)? Pin
Member 1539432815-Oct-21 9:28
Member 1539432815-Oct-21 9:28 
GeneralRe: How do I create xy plots with WPF APP (.NET Framework)? Pin
Gerry Schmitz15-Oct-21 10:03
mveGerry Schmitz15-Oct-21 10:03 
GeneralRe: How do I create xy plots with WPF APP (.NET Framework)? Pin
Member 1539432816-Oct-21 1:40
Member 1539432816-Oct-21 1:40 
GeneralRe: How do I create xy plots with WPF APP (.NET Framework)? Pin
Eddy Vluggen16-Oct-21 3:13
professionalEddy Vluggen16-Oct-21 3:13 
GeneralRe: How do I create xy plots with WPF APP (.NET Framework)? Pin
Gerry Schmitz16-Oct-21 9:46
mveGerry Schmitz16-Oct-21 9:46 
GeneralRe: How do I create xy plots with WPF APP (.NET Framework)? --- Solved Pin
Member 1539432817-Oct-21 4:44
Member 1539432817-Oct-21 4:44 
GeneralRe: How do I create xy plots with WPF APP (.NET Framework)? Pin
Gerry Schmitz17-Oct-21 11:30
mveGerry Schmitz17-Oct-21 11:30 
Question(xaml, wpf) ScrollViewer Binding confusion (beginner) Pin
Maximilien7-Oct-21 8:46
Maximilien7-Oct-21 8:46 
AnswerRe: (xaml, wpf) ScrollViewer Binding confusion (beginner) Pin
Richard Deeming7-Oct-21 21:47
mveRichard Deeming7-Oct-21 21:47 
AnswerRe: (xaml, wpf) ScrollViewer Binding confusion (beginner) Pin
Gerry Schmitz8-Oct-21 9:57
mveGerry Schmitz8-Oct-21 9:57 
QuestionCurrency Entry Issue Pin
Kevin Marois7-Oct-21 7:23
professionalKevin Marois7-Oct-21 7:23 
AnswerRe: Currency Entry Issue Pin
Richard Deeming7-Oct-21 21:44
mveRichard Deeming7-Oct-21 21:44 
GeneralRe: Currency Entry Issue Pin
Kevin Marois8-Oct-21 6:03
professionalKevin Marois8-Oct-21 6:03 
QuestionQuick MVVM Design Question Pin
Kevin Marois5-Oct-21 7:34
professionalKevin Marois5-Oct-21 7:34 
AnswerRe: Quick MVVM Design Question Pin
Mycroft Holmes5-Oct-21 13:39
professionalMycroft Holmes5-Oct-21 13:39 
AnswerRe: Quick MVVM Design Question Pin
Pete O'Hanlon5-Oct-21 20:26
mvePete O'Hanlon5-Oct-21 20:26 
GeneralRe: Quick MVVM Design Question Pin
Richard Deeming5-Oct-21 21:34
mveRichard Deeming5-Oct-21 21:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.