简体   繁体   中英

silverlight: Editing Textbox Value in combobox custom item template

I have created a custom item template for my Combobox which has a textbox in it. I can type in the textbox when the combobox is open (being dropped down) but as soon as the item is selected and the dropdown has closed, i can no longer write in the text box.

<ComboBox ItemsSource="{Binding CriteriaCollection}" Margin="2,0,5,5" Height="35" Grid.Column="1">
        <ComboBox.ItemTemplate>
          <DataTemplate>
            <Grid Height="25" HorizontalAlignment="Stretch">
              <Grid.ColumnDefinitions>
                <ColumnDefinition Width="20" />
                <ColumnDefinition Width="150"/>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
              </Grid.ColumnDefinitions>
              <RadioButton Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center" IsChecked="{Binding IsSelected, Mode=TwoWay}" />
              <TextBlock Grid.Column="1" Text="{Binding DisplayText}" VerticalAlignment="Center"   />
              <TextBox Grid.Column="2" Text="{Binding Value,Mode=TwoWay}" VerticalAlignment="Center" MinWidth="{Binding ValueTextBoxMinWidth}" MaxWidth="{Binding ValueTextBoxMaxWidth}" HorizontalAlignment="Left"/>
              <TextBlock Grid.Column="3" Text="{Binding PostValueText}" VerticalAlignment="Center" Visibility="{Binding ComparatorVisibility}" Margin="5,0,5,0" HorizontalAlignment="{Binding PostValueTextHoirzontalAlignment}" />
              <TextBox Grid.Column="4" Text="{Binding UpperValue,Mode=TwoWay}" VerticalAlignment="Center" MinWidth="40" Visibility="{Binding UpperValueVisibility}" Margin="5,0,5,0"/>
            </Grid>
          </DataTemplate>
        </ComboBox.ItemTemplate>
      </ComboBox>

As you can see from the xaml above, I decide what to display in the template by the binding to some properties on my viewmodel, hence not just having a combobox with text in, then a seperate text box control beside it.

Is there any way to allow for editing the text in the combobox once the item has been selected?

Unfortunately, what is shown when the ComboBox is closed is completely unrelated to the ItemTemplate (which is what is shown when the dropdown is shown).

I think your only hope is to subclass ComboBox and re-template it. The new template would have a TextBox instead of a TextBlock inside the ContentPresenter. In your subclass, you'd set up a two-way binding between the text of the TextBox and the text of the selected item.

you can use custom template for your combobox on the whole as said by RobSiklos, but i think you can do that in xaml itself, writing custom template, use overridedefaultstyle property, use the Textbox instead of Textblock in the content presenter as again, said by RobSiklos.

Then you can also use Triggers like when IsSelected and other properties and handle your stuff over.

See this link for how to create a custom template and using similar one for your Combobox will hopefully help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM