1

I have this DataGrid I want to change the Disabled behaviour (xaml); I want to change this small part in the template.

If it is not possible i dont mind to use:

<Setter Property="OverridesDefaultStyle" Value="True"/>

And To replace the entire xaml (template) of my control, But I need the complete template to copy paste and help where to change the Disabled look-like part.

Can anyone help me?

Programer
  • 1,005
  • 3
  • 22
  • 46
  • 1
    Can you please explain what is it that you exactly want to achieve? You want to apply a new template to WPF DataGrid and you want the current control template of `DataGrid` to override on top of it? – WPF-it Mar 14 '12 at 09:51
  • to use the same template , only to change the IsEnabled foreground from gray to green (override this part) – Programer Mar 14 '12 at 14:01

2 Answers2

2

EDIT: It's been pointed out that all the default control templates are available at MSDN which makes the below relevant, but I'll leave it here for interest.

Given an instance of a control you get serialize the markup for the control template using the System.Windows.Markup.XamlWriter class.

To get a control template:

string markup = System.Windows.Markup.XamlWriter.Save(control.Template);

To get a complete dump (including triggers etc) of the control template use.

        StringBuilder markupBuilder = new StringBuilder();

        XmlWriter writer = XmlWriter.Create(markupBuilder);

        System.Windows.Markup.XamlDesignerSerializationManager manager = 
            new System.Windows.Markup.XamlDesignerSerializationManager(writer);

        manager.XamlWriterMode = System.Windows.Markup.XamlWriterMode.Value;

        // data grid named dataGrid1
        var template = dataGrid1.Template;

        System.Windows.Markup.XamlWriter.Save(dataGrid1.Template, manager);

        string markup = markupBuilder.ToString();

If you just looking to change the foreground color of the DataGrid when it's disabled, you should be able to use styles together with triggers rather than replacing the entire template.

<DataGrid>
    <DataGrid.Resources>
        <Style
            TargetType="{x:Type DataGridColumnHeader}">
            <Style.Triggers>
                <DataTrigger
                    Binding="{Binding IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                    Value="False">
                    <Setter
                        Property="Foreground"
                        Value="Green" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
        <Style
            TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <DataTrigger
                    Binding="{Binding IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}"
                    Value="False">
                    <Setter
                        Property="Foreground"
                        Value="Green" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.Resources>
    <!-- Column Definitions -->
</DataGrid>

Adding the above 2 styles to the DataGrid's resources collection will, set the foreground of each column header and data row cell to green when the DataGrid is disabled.

Luke Forder
  • 1,179
  • 14
  • 20
  • That kind of work is unnecessary, you can get all the templates on MSDN, see http://stackoverflow.com/questions/1559261/control-template-for-existing-controls-in-wpf – H.B. Mar 14 '12 at 13:11
  • @H.B. Oh... I didn't know. Definitely easier :) – Luke Forder Mar 14 '12 at 13:18
  • 1
    Also, Expression Blend allows you to create copies of the templates of any control... – myermian Mar 14 '12 at 13:46
  • Thanks. I tried: than pasting your xaml above but it destroys my grid, meaning it does not function as before - no rows are visible etc. – Programer Mar 14 '12 at 14:15
  • Place the DataGrid.Resources element directly within the DataGrid element. – Luke Forder Mar 14 '12 at 14:56
  • I did not put it into the dataGrid, I added the two styles sepratley in my ResourceDictionary (under DataGrid.Resource it throws an exception). ThankS! – Programer Mar 14 '12 at 14:57
0

To define the disabled behavior of any control, you should change the Disabled visual state accordingly in the control template.

andrew
  • 1,030
  • 2
  • 8
  • 21
  • i dont see it...is the one posted here correct? http://social.msdn.microsoft.com/Forums/en/wpf/thread/8e6f3c70-f65f-4152-9469-9ae3e9239126 if so where is the part you said? – Programer Mar 14 '12 at 10:00