Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. DataGridCell Style Problem

DataGridCell Style Problem

Scheduled Pinned Locked Moved WPF
helpwpfquestioncsharpwcf
1 Posts 1 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I'm creating a WPF DataGrid. The data source is a DataTable created in the VM. [See this pic](https://1drv.ms/u/s!AjBmoYAYz\_v2hBJl1Yj0BBLCyDbW?e=gcMT3H). 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()
    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.

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups