Click here to Skip to main content
15,881,281 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Kevin Marois28-Dec-21 10:25
professionalKevin Marois28-Dec-21 10:25 
GeneralRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Super Lloyd16-Jan-22 6:45
Super Lloyd16-Jan-22 6:45 
GeneralRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Mycroft Holmes16-Jan-22 10:55
professionalMycroft Holmes16-Jan-22 10:55 
GeneralRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Eddy Vluggen9-Feb-22 2:40
professionalEddy Vluggen9-Feb-22 2:40 
GeneralRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Super Lloyd9-Feb-22 12:10
Super Lloyd9-Feb-22 12:10 
GeneralRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Eddy Vluggen9-Feb-22 12:13
professionalEddy Vluggen9-Feb-22 12:13 
AnswerRe: TextBox Bound To Decimal - Can't Type Decimal Pin
Richard Deeming4-Jan-22 0:04
mveRichard Deeming4-Jan-22 0:04 
QuestionSetting DataGrid Cell Style From Code Behind Pin
Kevin Marois20-Dec-21 10:56
professionalKevin Marois20-Dec-21 10:56 
I have a UserControl with a data grid that I'm creating in code in a View Model. It looks like this [^]. I'm trying to set the Cell Style to show 2 decimal from the VM but you can see it's not working.

The first column is a string, and the remainder are Decimal.

Here's my Cell Style
<Style x:Key="dataGridCurrencyCellStyle" 
        BasedOn="{StaticResource dataGridCellStyle}"
        TargetType="{x:Type DataGridCell}">

    <Setter Property="Width" Value="75"/>
    <Setter Property="TextBlock.TextAlignment" Value="Right"/>
    <Setter Property="TextBlock.Text" Value="{Binding StringFormat={}{0:0.##}}"/>
    <Setter Property="IsEnabled" Value="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}"/>

</Style>

Here's how I'm creating the table in the VM and setting the Style. The Style is set in the foreach loop towards the bottom.
private void LoadTable()
{
    if (Budgets.Count > 0)
    {
        // 1- Create the data table

        BudgetTable = null;
        BudgetTable = new DataTable();

        // Add the first column
        DataColumn column = new DataColumn();
        column.ColumnName = "Type";
        column.DataType = System.Type.GetType("System.String");
        column.ReadOnly = false;
        BudgetTable.Columns.Add(column);

        // Get a distinct list of Plan/Elevations
        List<string> planElevations = new List<string>();

        switch (BudgetType)
        {
            case BudgetType.Job:
                var sheets = JobSequenceSheets.Where(x => x.Status == "New").ToList();
                planElevations = sheets.Select(x => $"{x.Plan}{x.Elevation}").ToList();
                break;

            case BudgetType.Project:
                planElevations = ProjectPlanTypeSummaries.Select(x => $"{x.Plan}{x.Elevation}").ToList();
                break;

            default:
                throw new ArgumentException("Unhandled CASE statement");
                break;
        }

        // Add a column for every distinct Plan/Elevation
        foreach (var planElevation in planElevations)
        {
            column = new DataColumn();

            column.ColumnName = planElevation;
            column.Caption = planElevation;
            column.DataType = System.Type.GetType("System.Decimal");
            column.ReadOnly = false;

            BudgetTable.Columns.Add(column);
        }

        // 2 - Now Bind Columns

        // Set up the columns list
        DataGridColumns = new ObservableCollection<DataGridColumn>();

        // Get a list of column names from the Budget Table
        string[] columnNames = (from dc in BudgetTable.Columns.Cast<DataColumn>()
                                select dc.ColumnName).ToArray();

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

            // Get binding mode and style name
            if (item == "Type")
            {
                binding.Mode = BindingMode.OneTime;
                styleName = "budgetGridCellStyle";
            }
            else
            {
                binding.Mode = BindingMode.TwoWay;
                binding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                styleName = "dataGridCurrencyCellStyle";
            }

            // Get the style
            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);
        }

        // 3 - Now add rows for the data
        foreach (var budget in Budgets)
        {
            AddRow(budget);
        }
    }
}
DataGrid XAML
<DataGrid Grid.Row="1" 
            x:Name="budgetGrid" 
            Style="{StaticResource dataGridStyle}"
            GridLinesVisibility="All"
            ItemsSource="{Binding BudgetTable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

    <i:Interaction.Behaviors>
        <cls:ColumnsBindingBehaviour Columns="{Binding DataContext.DataGridColumns, 
                                                        Mode=TwoWay, 
                                                        UpdateSourceTrigger=PropertyChanged, 
                                                        RelativeSource={RelativeSource AncestorType=DataGrid}}" />
    </i:Interaction.Behaviors>

    <behaviours:Interaction.Triggers>
        <behaviours:EventTrigger EventName="CellEditEnding">
            <behaviours:InvokeCommandAction Command="{Binding ElementName=budgetControl, Path=DataContext.CellEditedCommand}" 
                                            PassEventArgsToCommand="True"/>
        </behaviours:EventTrigger>
    </behaviours:Interaction.Triggers>

</DataGrid>

What's the right way to do this?

Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

AnswerRe: Setting DataGrid Cell Style From Code Behind Pin
#realJSOP22-Dec-21 2:06
mve#realJSOP22-Dec-21 2:06 
QuestionInput Question Pin
Super Lloyd19-Dec-21 4:17
Super Lloyd19-Dec-21 4:17 
AnswerRe: Input Question Pin
#realJSOP19-Dec-21 22:26
mve#realJSOP19-Dec-21 22:26 
GeneralRe: Input Question Pin
Super Lloyd19-Dec-21 23:05
Super Lloyd19-Dec-21 23:05 
GeneralRe: Input Question Pin
#realJSOP19-Dec-21 23:18
mve#realJSOP19-Dec-21 23:18 
GeneralRe: Input Question Pin
Super Lloyd19-Dec-21 23:23
Super Lloyd19-Dec-21 23:23 
Question(Advanced?) Layout question Pin
Super Lloyd17-Dec-21 20:02
Super Lloyd17-Dec-21 20:02 
QuestionAbout RichTextBox size in a canvas Pin
Super Lloyd12-Dec-21 1:23
Super Lloyd12-Dec-21 1:23 
AnswerRe: About RichTextBox size in a canvas Pin
Richard Deeming12-Dec-21 21:49
mveRichard Deeming12-Dec-21 21:49 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd12-Dec-21 21:58
Super Lloyd12-Dec-21 21:58 
AnswerRe: About RichTextBox size in a canvas Pin
Gerry Schmitz13-Dec-21 7:22
mveGerry Schmitz13-Dec-21 7:22 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd13-Dec-21 11:42
Super Lloyd13-Dec-21 11:42 
GeneralRe: About RichTextBox size in a canvas Pin
Gerry Schmitz13-Dec-21 14:28
mveGerry Schmitz13-Dec-21 14:28 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd13-Dec-21 14:40
Super Lloyd13-Dec-21 14:40 
GeneralRe: About RichTextBox size in a canvas Pin
Gerry Schmitz14-Dec-21 7:28
mveGerry Schmitz14-Dec-21 7:28 
GeneralRe: About RichTextBox size in a canvas Pin
Super Lloyd14-Dec-21 12:28
Super Lloyd14-Dec-21 12:28 
AnswerRe: About RichTextBox size in a canvas Pin
Mycroft Holmes13-Dec-21 13:16
professionalMycroft Holmes13-Dec-21 13:16 

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.