简体   繁体   English

silverlight:在组合框自定义项模板中编辑文本框值

[英]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. 从上面的xaml中可以看到,我通过绑定到ViewModel上的某些属性来决定在模板中显示什么,因此,不仅要有一个带有文本的组合框,还要在旁边有一个单独的文本框控件。

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). 不幸的是,关闭ComboBox时显示的内容与ItemTemplate完全无关(显示下拉列表时即显示的内容)。

I think your only hope is to subclass ComboBox and re-template it. 我认为您唯一的希望是继承ComboBox并重新创建模板。 The new template would have a TextBox instead of a TextBlock inside the ContentPresenter. 新模板在ContentPresenter内部将具有TextBox而不是TextBlock。 In your subclass, you'd set up a two-way binding between the text of the TextBox and the text of the selected item. 在子类中,您将在TextBox的文本和所选项目的文本之间建立双向绑定。

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. 您可以按照RobSiklos的说明在整个组合框中使用自定义模板,但是我认为您可以在xaml本身中做到这一点,编写自定义模板,使用overridedefaultstyle属性,在内容演示者中再次使用Textbox而不是Textblock, RobSiklos。

Then you can also use Triggers like when IsSelected and other properties and handle your stuff over. 然后,您还可以使用触发器(如IsSelected时)和其他属性来处理您的工作。

See this link for how to create a custom template and using similar one for your Combobox will hopefully help you. 有关如何创建自定义模板的信息,请参见此链接 ,并为您的组合框使用类似的模板希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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