Hi,
I have a XamComboEditor which is bound to an ItemsSource which has a list of custom Entity instances. Entity has an integer Id and string Code properties. I set the ValuePath="Id" and DisplayMemberPath="Code". And I also set the Value property of combo to a property named "SelectedValue" on window in order to explain my problem.
The thing when the SelectedValue is 0, so the Value of Combo, the display text on XamComboEditor shows "0" since there is no Entity with Id = 0 in ItemsSource. What I want the combo is to show NOT 0 but empty string when the value is not found in ItemsSource. There is an option for that in some components but couldn't find on XamComboEditor. I've also tried to use ValueToTextConverter and ValueToDisplayTextConverter to convert 0 to String.Empty but they didn't work either.
I've attached the sample project in order to show the problem.
Can somebody help me out?
Thanks in advance
Hello,
This looks like an issue in the XamComboEditor. This should be achievable with the ValueToDisplayTextConverter and I see it is being called when the editor loads, but the text is not being changed.
As a temporary solution, you could use the ValueChanged event of the XamComboEditor and check if the e.NewValue is 0 and change the Text property of the editor. Please note that this will probably throw some exceptions (silently) saying that a string cannot be converter to an int32 as the XamComboEditor is expecting an integer rather than a string.
private void XamComboEditor_ValueChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
if (e.NewValue is int && (int)e.NewValue == 0)
(sender as XamComboEditor).Text = "null text";
}
Another way would be to place the text in the adorner layer of the XamComboEditor when it's value is equal to 0.
Thanks for the reply!
Since I'm using MVVM with DataTemplates I try not to use events as long as they are not needed. And in this case I would like to use your second solution with an adorner layer but I don't know how I can do that.
Can you send me small working sample?
And since this is common usage scenario with ComboBox'es, shouldn't there be a property/option to set the display text for unrecognized values (values not found in list)? Will this be added to your to-do list?
Thanks
Actually, looking at it again it seems that there is a simpler solution. What you can do is to put an element in the control template of the XamComboEditor and hide the ContentPresenter, which presents the value with a trigger. I am attaching the full style for the XamComboEditor with the made modifications :
<Border Grid.Column="0" Margin="1,0,0,0" Padding="{TemplateBinding Padding}">
<Grid>
<ContentPresenter x:Name="PART_Content" .../>
<TextBlock x:Name="PART_NullText" Text="Null text" Visibility="Collapsed"/>
</Grid>
</Border>
And in the ControlTemplateTriggers, you can add the following trigger :
<Trigger Property="Content" SourceName="PART_Content" Value="0">
<Setter Property="Visibility" Value="Hidden" TargetName="PART_Content"/>
<Setter Property="Visibility" Value="Visible" TargetName="PART_NullText"/>
</Trigger>
[Edit] : I have already created a support case on your behalf so that we resolve the issue with the display text converter. You will be notified when this is resolve and the service release is out through the case (CAS-51522-Y10PJ4)
First of all thanks for creating the support case.
I have applied the style you've given and it worked! But when I use XamComboEditor or ComboEditorTool on a ribbon this style doesn't work. I don't understand why.
Can you check out the sample project that I've attached? I've shown the problematic cases in the project.
Thanks for the reply.
I will check out the article you've mentioned.
For using the same style for XamComboEditor also for ComboBoxEditor, I assumed that, since ComboBoxEditor derives from XamComboEditor, it was also inheriting the style. Apparently it is not.
Thanks again
There are two main issues here that prevent the correct behavior. The first is that you are using a theme for the XamRibbon. By setting the theme, a resource dictionary with the theme styles is placed inside the Resources of the control. This prevents the WPF from finding the style that you have defined in the resource dictionary (merged in the App.xaml). In this case, you would either have to put the style in the XamRibbon's resources or set it explicitly using {StaticResource...}.
The second issue is that you have created a style for the XamComboEditor only and you are using both XamComboEditor and ComboBoxTool. A style will be picked up only by those elements that match the TargetType of the style - which in your case would be for the editor only.
A much better description for these issues you can find in this blog post by Andrew Smith which I recommend that you go over. Reading this, you should have no problems resolving this.