Stojdza
Stojdza

Reputation: 445

How to access combobox inside control template in WPF?

I have a control template defined like this:

<Window.Resources>
 <ControlTemplate x:Key="fiscalItemsControlTemplate">
  <Grid Grid.Column="2">
           <Grid.RowDefinitions>
               <RowDefinition Height="17" />
               <RowDefinition Height="19" />
               <RowDefinition Height="17" />
               <RowDefinition Height="19" />
               <RowDefinition Height="17" />
               <RowDefinition Height="19" />
           </Grid.RowDefinitions>
      <Label Padding="0"  Grid.Row="0" Content="{DynamicResource AmmountStr}" HorizontalAlignment="Left" Name="lblAmmount" VerticalAlignment="Bottom" Height="17"/>
      <TextBox Padding="0"  Name="txtAmmount" Grid.Row="1" Height="19" Width="189" HorizontalAlignment="Left" VerticalAlignment="Bottom" Text="{Binding Path=Amount, Converter={StaticResource moneyConverter}}" />
      <Label Padding="0"  Content="PurchasePrice" Grid.Row="2" Grid.RowSpan="2" Height="17" HorizontalAlignment="Left" Name="lblPurchasePrice" VerticalAlignment="Top" />
      <TextBox Padding="0"  Grid.Row="3" Grid.RowSpan="2" Height="19" HorizontalAlignment="Left" Name="txtPurchasePrice" VerticalAlignment="Top" Width="189" Text="{Binding Path=PurchasePrice, Converter={StaticResource moneyConverter}, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnSourceUpdated=True}" Validation.Error="Validation_Error" PreviewTextInput="NumericOnly" />
      <Label Padding="0" Grid.Row="4" Name="lblOrderState" HorizontalAlignment="Left" Content="Order State" Height="17" />
      <ComboBox Padding="0" Grid.Row="5" HorizontalAlignment="Left" Name="cbOrderState" Height="19" Width="189" >
      </ComboBox>
  </Grid>
 </ControlTemplate>
</Window.Resources>

What I'm trying to do is to access combobox "cbOrderState" in codebehind and declare it's itemssours there. I know there are some ways with the FindName() method but how to use it when control template is defined in Window.Resources?

Upvotes: 0

Views: 734

Answers (2)

Rohit Vats
Rohit Vats

Reputation: 81263

First of all it's not a good practise to access template and set it's properties from code behind when you have binding already to support that.

Now, even in case you want to do that FindName() is a way to go. You need to access Template from the control on which this resource is applied.

Say you have comboBox declared like this:

<ComboBox x:Name="cmb" Template="{StaticResource fiscalItemsControlTemplate}"/>

You can access from code behind like this:

var comboBox = cmb.Template.FindName("cbOrderState", cmb);

Upvotes: 0

PakKkO
PakKkO

Reputation: 154

You can use a CollectionViewSource:

<Window.Resources>
        <CollectionViewSource x:Key="ViewName"/>
</Window.Resources>

and use in your combobox:

<ComboBox Padding="0" Grid.Row="5" HorizontalAlignment="Left" Name="cbOrderState" Height="19" Width="189" ItemsSource="{Binding Source={StaticResource ViewName}}" >

and populate data in codebehind:

CollectionViewSource yourView = ((CollectionViewSource)(this.FindResource("ViewName")));

yourView.Source = yourCollection;

Upvotes: 1

Related Questions