Frage

I have a ControlTemplate declared in UserControl.Resources. So I know how to add that control to my control(DataGrid in this case) like this:

MyDataGrid.Template = (ControlTemplate)this.FindResource("inputItemsControlTemplate");

Now I want to remove this ControlTemplate in code behind. Actually what I'm trying to manage is to change the look of DataGrid to it's previous state, as it was before this line of code.

Update:

Ok I tried to be as simpler as possible but I'll describe the full stuff here. I have a datagrid defined with all of it's binding and event handlers and all the other stuff. Also I have a ControlTemplate defined in my . This ControlTemplate is actualy a form which I use for inserting new data to datagrid. Here is the code of my template:

<ControlTemplate x:Key="inputItemsControlTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="120"/>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition Width="200"/>
            </Grid.ColumnDefinitions>
            <Grid Grid.Column="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                </Grid.RowDefinitions>
                <Label Grid.Row="1" Content="{DynamicResource UCodeStr}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Height="27" />
                <TextBox Name="txtUCode" Grid.Row="2" Height="23" HorizontalAlignment="Left" VerticalAlignment="Bottom"  Width="100" Text="{Binding UCode, UpdateSourceTrigger=PropertyChanged}" />
            </Grid>
            <Grid Grid.Column="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                </Grid.RowDefinitions>
                <Label Grid.Row="1" Content="{DynamicResource GoodStr}" HorizontalAlignment="Left" VerticalAlignment="Bottom" Height="27"/>
                <ComboBox Grid.Row="2" Height="23" Width="189" HorizontalAlignment="Left"  Name="cbGoods" ItemsSource="{Binding Path=Goods}"  SelectedItem="{Binding Path= Good, UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Name" IsEditable="True" IsTextSearchEnabled="True" TextSearch.TextPath="Name" />
                <Label Grid.Row="3" Content="{DynamicResource AmmountStr}" HorizontalAlignment="Left" Name="lblAmmount" VerticalAlignment="Bottom" Height="27"/>
                <TextBox Name="txtAmmount" TextAlignment="Right"  Grid.Row="4" Height="23" Width="189" HorizontalAlignment="Left" VerticalAlignment="Bottom" Text="{Binding Path=Amount,  ValidatesOnDataErrors=True, NotifyOnValidationError=True,  UpdateSourceTrigger=PropertyChanged, StringFormat='N2'}" />
            </Grid>
            <Grid Grid.Column="2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                    <RowDefinition Height="25" />
                </Grid.RowDefinitions>
                <Label Grid.Row="1" Content="{DynamicResource InputPriceStr}" HorizontalAlignment="Left" Name="lblInputPrice" VerticalAlignment="Bottom" Height="27"/>
                <TextBox Name="txtInputPrice" Grid.Row="2" TextAlignment="Right" Height="23" Width="189" HorizontalAlignment="Left" VerticalAlignment="Bottom" Text="{Binding Path= InputPrice, UpdateSourceTrigger=PropertyChanged,  ValidatesOnDataErrors=True, NotifyOnValidationError=True, StringFormat='N2'}" />
                <Label Grid.Row="3" Content="{DynamicResource SuggestedPriceStr}" HorizontalAlignment="Left" Name="lblSuggestedPrice" VerticalAlignment="Bottom" Height="27"/>
                <TextBox Name="txtSuggestedPrice" Grid.Row="4" TextAlignment="Right" Height="23" Width="189" HorizontalAlignment="Left" VerticalAlignment="Bottom" Text="{Binding Path= SuggestedPrice,  ValidatesOnDataErrors=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, StringFormat='N2'}" />
                <CheckBox Grid.Row="5" Name="cbHasVatDeduction"  IsChecked="{Binding Path=HasVatDeduction}" />
            </Grid>
        </Grid>
    </ControlTemplate>

I have some add button. In it's Click Event Handler I do this:

MyDataGrid.Template = (ControlTemplate)this.FindResource("inputItemsControlTemplate");

The effect I get with this is my DataGrid is replaced with those textboxes and combobox for inserting new data. What I want to do is to switch back to my default datagrid look without using another template. I'm doing that right now but it is not good for me becouse the events in my datagrid. Here is the code of my datagrid:

<DataGrid AutoGenerateColumns="False"
                                      IsReadOnly="True"
                                      Name="InputDocItemsDataGrid"
                                      ItemsSource="{Binding Path= InputItems}" 
                                      SelectedItem="{Binding Path= InputItem, UpdateSourceTrigger=PropertyChanged}"
                                      SelectionChanged="InputDocItemsDataGrid_SelectionChanged"
                                      PreviewMouseLeftButtonDown="InputDocItemsDataGrid_PreviewMouseLeftButtonDown">
                            <DataGrid.Columns>
                                <DataGridTemplateColumn CanUserReorder="False" CanUserResize="False">
                                    <DataGridTemplateColumn.HeaderTemplate>
                                        <DataTemplate>
                                            <CheckBox Name="cbxAll" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Checked="cbxAll_Checked" />
                                        </DataTemplate>
                                    </DataGridTemplateColumn.HeaderTemplate>
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <CheckBox Name="cbxSingleRow" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" PreviewMouseLeftButtonDown="cbxSingleRow_PreviewMouseLeftButtonDown" />
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                                <DataGridTextColumn Header="{DynamicResource StockStr}" Binding="{Binding Path= tblInputDoc.tblStock.Name}"  />
                                    <DataGridTextColumn Header="{DynamicResource UCodeStr}" Binding="{Binding Path= tblGood.UCode}" />
                                    <DataGridTextColumn Header="{DynamicResource GoodStr}" Binding="{Binding Path= tblGood.Name}" />
                                    <DataGridTextColumn Header="{DynamicResource AmmountStr}" Binding="{Binding Path= Amount, Converter={StaticResource moneyConverter}}" />
                                    <DataGridTextColumn Header="{DynamicResource InputPriceStr}" Binding="{Binding Path= InputPrice, Converter={StaticResource moneyConverter}}" />
                                    <DataGridTextColumn Header="{DynamicResource SuggestedPriceStr}" Binding="{Binding Path= SuggestedPrice, Converter={StaticResource moneyConverter}}" />
                                    <DataGridTextColumn Header="{DynamicResource InputValueStr}" Binding="{Binding Path= InputValue, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
                                    <DataGridTextColumn Header="{DynamicResource VatBaseStr}" Binding="{Binding Path= VatBase, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
                                    <DataGridTextColumn Header="{DynamicResource VatValueStr}" Binding="{Binding Path= VatValue, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
                                    <DataGridTextColumn Header="{DynamicResource InputWithVatStr}" Binding="{Binding Path= InputPriceWithVat, Mode=OneWay, Converter={StaticResource moneyConverter}}" />

                                    <!--<DataGridTextColumn Header="Stock" Binding="{Binding Path= tblStock.Name}" />-->
                                </DataGrid.Columns>
                            </DataGrid>

And the code of my second ControlTemplate which I use to switch to previous look of datagrid:

<ControlTemplate x:Key="baseDataGridTemplate">
        <DataGrid AutoGenerateColumns="False"
                                      IsReadOnly="True"
                                      Name="InputDocItemsDataGrid"
                                      ItemsSource="{Binding Path= InputItems}" 
                                      SelectedItem="{Binding Path= InputItem, UpdateSourceTrigger=PropertyChanged}"
                                      SelectionChanged="InputDocItemsDataGrid_SelectionChanged"
                                      PreviewMouseLeftButtonDown="InputDocItemsDataGrid_PreviewMouseLeftButtonDown">
            <DataGrid.Columns>
                <DataGridTemplateColumn CanUserReorder="False" CanUserResize="False">
                    <DataGridTemplateColumn.HeaderTemplate>
                        <DataTemplate>
                            <CheckBox Name="cbxAll" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </DataTemplate>
                    </DataGridTemplateColumn.HeaderTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox Name="cbxSingleRow" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="{DynamicResource StockStr}" Binding="{Binding Path= tblInputDoc.tblStock.Name}"  />
                <DataGridTextColumn Header="{DynamicResource UCodeStr}" Binding="{Binding Path= tblGood.UCode}" />
                <DataGridTextColumn Header="{DynamicResource GoodStr}" Binding="{Binding Path= tblGood.Name}" />
                <DataGridTextColumn Header="{DynamicResource AmmountStr}" Binding="{Binding Path= Amount, Converter={StaticResource moneyConverter}}" />
                <DataGridTextColumn Header="{DynamicResource InputPriceStr}" Binding="{Binding Path= InputPrice, Converter={StaticResource moneyConverter}}" />
                <DataGridTextColumn Header="{DynamicResource SuggestedPriceStr}" Binding="{Binding Path= SuggestedPrice, Converter={StaticResource moneyConverter}}" />
                <DataGridTextColumn Header="{DynamicResource InputValueStr}" Binding="{Binding Path= InputValue, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
                <DataGridTextColumn Header="{DynamicResource VatBaseStr}" Binding="{Binding Path= VatBase, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
                <DataGridTextColumn Header="{DynamicResource VatValueStr}" Binding="{Binding Path= VatValue, Mode=OneWay, Converter={StaticResource moneyConverter}}" />
                <DataGridTextColumn Header="{DynamicResource InputWithVatStr}" Binding="{Binding Path= InputPriceWithVat, Mode=OneWay, Converter={StaticResource moneyConverter}}" />

                <!--<DataGridTextColumn Header="Stock" Binding="{Binding Path= tblStock.Name}" />-->
            </DataGrid.Columns>
        </DataGrid>
    </ControlTemplate>

I really hope you get the point of what I'm trying to do. I can't be more specific than this and I wanted to avoid the amount of code in the question. If this is not enoughf than the question should be deleted.

War es hilfreich?

Lösung

I have no idea what you talking about it seems like you got super lost in wpf's world :)

Take a look at this:

this.previousTemplate = MyDataGrid.Template;
MyDataGrid.Template = (ControlTemplate)this.FindResource("baseDataGridTemplate");

and then at some point later...

MyDataGrid.Template = previousTemplate;

Andere Tipps

If you want to do this through code behind then before you assign your custom ControlTemplate save your MyDataGrid let's say

var defaultDataGrid = MyDataGrid;
//then
MyDataGrid.Template = (ControlTemplate)this.FindResource("baseDataGridTemplate");
//and when you want to change it back
MyDataGrid.Template = defaultDataGrid.Template

let us know how it went :-)

But to be perfectly honest you could do this in xaml using DataTriggers in DataGrid Style

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top