WPF TextBox with both static and editable text?
I have a data entry application, with (among other things) a textbox for recording comments. These comments are specific to the data being entered, and often times are redundant (same comment given for a range of data records).
I'm planning to add a combobox with a canned selection of comments to cover the most common situations. When one is selected from the combobox, the comment textbox is populated with the canned comment.
However, I also need the ability to enter additional comments after the canned message, within the textbox. But I don't wa开发者_开发问答nt it to be possible for the canned message to be altered. All entered comments need to come after the canned comment.
Is there a way to apply static text to a textbox which cannot be altered, but still allow text to be entered below it?
The only idea I've come up with so far is to catch TextInput events and continually overwrite the beginning of the textbox content with the canned message, but the result wouldn't exactly be pretty.
You could just place a disabled TextBox
immediately above the editable TextBox
and remove their bottom and top borders respectively so that they look like one big TextBox
.
Perhaps overwrite a TextBox
template so that it contains a Panel
with the Canned Message ComboBox
and a regular TextBox
for user input.
Style the inner TextBox
so it doesn't have the regular TextBox border, and style the ComboBox
so that when it doesn't have focus it doesn't show it's border either.
When the ComboBox has focus, it will look like a ComboBox inside a TextBox, and if it doesn't have focus it will just look like one big TextBox
No, as much I aware of , you can not have something like this. But you can:
- Have a label on top (side) of it with static text applied
- If you have enough space, have a readonlt text box for preview of the comment, more or less like SO comment editor works.
- You can try to not let to delete several count of characters from binded data ( which will be actually static text)
Hope this helps.
I would adopt a slightly different strategy.
If something has been selected in the Combobox, then concatenate the input in the selected combo-box item text and the textbox text. If not, use the textbox text.
Of course you could use the selected event of the combobox to chnage the label to reflect the change in circumstance.
I think you can bind the textBox1.Text with the combobox selected item as Oneway mode. The following is code snipet
<ComboBox Height="23" HorizontalAlignment="Left" Margin="118,48,0,0" Name="comboBox1" VerticalAlignment="Top" Width="144">
<ComboBoxItem Content="Commanet 1" />
<ComboBoxItem Content="Comment 2" />
</ComboBox>
<TextBox Height="64" HorizontalAlignment="Left" Margin="118,101,0,0" Name="textBox1" VerticalAlignment="Top" Width="144" Text="{Binding ElementName=comboBox1, Path=SelectedItem.Content, Mode=OneWay}" />
2 more options.
- Override the style of the textbox to include the fixed text - pass the fixed text though some templatebinding
- You can capture the PreviewKey<> events on the textbox and cancel it its modifying the "fixed" text, and if not let the event go through.
But 2 textboxes that visually look as 1 is still a better option though - easiest to implement and maintain
精彩评论