WPF ComboBox IsEditable at wpf - wpf

I have a wpf (and ef) ComboBox that contains two fields first Name and Last name, everything works correctly
,
but I want them to look inside the box (the same idea as IsEditable="True"), but for some reason I can not
There is a solution with TextSearch.TextPath="FirstName",
But every choice appears only the first part which is not what I want, I would also LastName
I add the code
<ComboBox Name="comboBox1" VerticalAlignment="Top"
SelectedValuePath="CustomerId">
<ComboBox.ItemTemplate>
<DataTemplate >
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" >
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="FirstName" />
<Binding Path="LastName" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
And this
BeEntities bmContex = new BeEntities();
public aaa()
{
InitializeComponent();
var t = bmContex.Customers.ToList();
comboBox1.ItemsSource = t;
}
I would appreciate any help

Related

Display different values in ComboBox based on a conditional from converter

I have a ComboBox which should display different values based on a condition.
If the SelectedValue's properties Name or Email is not null and is not empty I want to display those.
If above is not true I want to fallback and display the SelectedValue's Username, this is never null or empty.
This is what I have so far.
<ctrls:RadComboBoxExtended Grid.Row="0" Grid.Column="1" ItemsSource="{Binding CurrentValue.LIST_OF_USERS}" SelectedValue="{Binding CurrentValue.User}" focus:FocusExtension.IsFocused="{Binding Focuspoint}">
<ctrls:RadComboBoxExtended.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Visibility>
<MultiBinding Converter="{StaticResource MultiNullOrEmptyStrToVisibility}" ConverterParameter="1">
<Binding Path="Name"/>
<Binding Path="Email"/>
</MultiBinding>
</TextBlock.Visibility>
<Run Text="{Binding Name}"/>
<Run Text="("/><Run Text="{Binding Email}"/><Run Text=")"/>
</TextBlock>
</DataTemplate>
</ctrls:RadComboBoxExtended.ItemTemplate>
</ctrls:RadComboBoxExtended>
Above XAML displays all users which have a Name and Email correctly.
How would I go about displaying the "else branch" for this? As of now the users which do not meet the condition of my converter is blank in the ComboBox. How can I display those?
Simplest way is to surround TextBlock with StackPanel or Grid and put also another TextBlock there, setting ConverterParameter="10" and handling it so, that only one of TextBlocks will be visible.
Another possibility is to use ItemTemplateSelector to define which DataTemplate should be used if RadComboBoxExtended has such a property.
<ctrls:RadComboBoxExtended Grid.Row="0" Grid.Column="1" ItemsSource="{Binding CurrentValue.LIST_OF_USERS}" SelectedValue="{Binding CurrentValue.User}" focus:FocusExtension.IsFocused="{Binding Focuspoint}">
<ctrls:RadComboBoxExtended.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock>
<TextBlock.Visibility>
<MultiBinding Converter="{StaticResource MultiNullOrEmptyStrToVisibility}" ConverterParameter="1">
<Binding Path="Name"/>
<Binding Path="Email"/>
</MultiBinding>
</TextBlock.Visibility>
<Run Text="{Binding Name}"/>
<Run Text="("/><Run Text="{Binding Email}"/><Run Text=")"/>
</TextBlock>
<TextBlock Text="{Binding Username}">
<TextBlock.Visibility>
<MultiBinding Converter="{StaticResource MultiNullOrEmptyStrToVisibility}" ConverterParameter="10">
<Binding Path="Name"/>
<Binding Path="Email"/>
</MultiBinding>
</TextBlock.Visibility>
</TextBlock>
</StackPanel>
</DataTemplate>
</ctrls:RadComboBoxExtended.ItemTemplate>
</ctrls:RadComboBoxExtended>
I would put the choice between name+email vs username in the ViewModel. (One of ViewModel responsibilities is to provide data for View in a convenient format)
public string SelectionText => String.IsNullOrEmpty(Name) && String.IsNullOrEmpty(Email)
? Username
: $"{Name} ({Email})" ;
much shorter DataTemplate and MultiValueConverter is not required
<ctrls:RadComboBoxExtended Grid.Row="0" Grid.Column="1" ItemsSource="{Binding CurrentValue.LIST_OF_USERS}" SelectedValue="{Binding CurrentValue.User}" focus:FocusExtension.IsFocused="{Binding Focuspoint}">
<ctrls:RadComboBoxExtended.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding SelectionText}"/>
</DataTemplate>
</ctrls:RadComboBoxExtended.ItemTemplate>
</ctrls:RadComboBoxExtended>

MultiBinding ComboBoxItem.Content

Hi I am trying to achieve a binding like this:
<ComboBoxItem Style="{StaticResource ComboBoxItemStyle2}">
<ComboBoxItem.Content>
<MultiBinding StringFormat=" {}{0} {1}">
<Binding Path="Value" Source="{StaticResource Name}" />
<Binding Path="Name" Source="{StaticResource Person}" />
</MultiBinding>
</ComboBoxItem.Content>
</ComboBoxItem>
Where "Name" is a localized string and "Value" is used to get it's localized string.
for some reason this doesn't seems to work. I am getting empty string.
This might help you: String format using MultiBinding?
Taken from that post:
You are trying to bind a string to an object. But StringFormat requires its target to be a string type. Try putting a TextBlock in your content and bind your data to it.
Also put "" around Name.
Following is the corrected code:
<ComboBoxItem Style="{StaticResource ComboBoxItemStyle2}">
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="Value" Source="{StaticResource Name}" />
<Binding Path="Name" Source="{StaticResource Person}" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ComboBoxItem>
I need to fix two things:
Keep the content as TextBlock and do binding of text there.
Removed extra spacing from StringFormat i.e. " {}{0} {1}" ==> "{}{0} {1}".
You Can see this link i wish be helpful
https://social.msdn.microsoft.com/Forums/vstudio/en-US/32b81578-b201-4927-bdc2-ebb9a42ae303/comboboxdisplaymemberpath-and-multibinding

binding Textblock from two textboxex value in Xaml

I have two textbox txtFName and txtLName . Now I want to display txtFName - txtLName in textblock using binding.
For bind only txtFName I write below code:
<TextBlock x:Name="textblock" Text="{Binding ElementName=txtFName , Path=Text}" Margin="-3,-8,0,0"/>
But I want to display txtFName - txtLName in textblock using binding.
I do not want to write any code in code behind.
Thanks,
You can do that using MultiBinding:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}">
<Binding ElementName="txtFName" Path="Text"/>
<Binding ElementName="txtLName" Path="Text"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
The content of a TextBlock is composed of a collection of inline objects, e.g. Runs. Therefore, you can bind txtFName and txtLName to two different runs, e.g. like this:
<TextBlock x:Name="textblock">
<Run Text="{Binding ElementName=txtFName, Path=Text}"/>
<Run Text=" - "/>
<Run Text="{Binding ElementName=txtLName, Path=Text}"/>
</TextBlock>

WPF Object send itself as MultiBinding path

Basically what I need to know is how to send the source of an HierarchicalDataTemplate into a binding, this is what I have:
<HierarchicalDataTemplate DataType="{x:Type myModel:Person}">
<StackPanel Orientation="Horizontal">
<Image Source="Images\User.gif" />
<TextBlock Margin="5,0,0,0" Text="{Binding Name}" />
</StackPanel>
<HierarchicalDataTemplate.ItemsSource>
<MultiBinding Converter="{StaticResource PersonConverter}">
<Binding Path="Name" />
<!-- Here I need something like Binding Path="Self" so I can send the source of the binding (the "Person" object) -->
</MultiBinding>
</HierarchicalDataTemplate.ItemsSource>
</HierarchicalDataTemplate>
So my source is an object of type myModel:Person, I want to be able to send the object itself in the MultiBinding so the PersonConverter can use it.
Thanks for any help.
Wow, I did a crazy wild guess and it worked =S lol, here's the solution
<MultiBinding Converter="{StaticResource PersonConverter}">
<Binding Path="Name" />
<Binding Path="." /> <!-- this sends the source of the binding -->
</MultiBinding>
Thanks!

WPF Commands Buttons ListBoxItems

I Have a listbox defined like below :
<ListBox x:Name="lstMedias" ItemsSource="{Binding Medias}" Width="Auto" Height="Auto">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Name}" Tag="{Binding Name}" Click="Button_Click" Command="{Binding Path=LoadSimpleMoviePopupCommand}">
<Button.Resources>
<Converters:LoadMovieMultiConverter x:Key="LoadMovieMultiConverter" />
</Button.Resources>
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource LoadMovieMultiConverter}">
<MultiBinding.Bindings>
<Binding ElementName="DragDropCanvas" />
<Binding Path="Tag" RelativeSource="{RelativeSource Self}" />
</MultiBinding.Bindings>
</MultiBinding>
</Button.CommandParameter>
</Button>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When Trying to call command LoadSimpleMoviePopupCommand, command is not called, but when calling click event, event is raised.
Do You have an idea why ? It is a normal behavior ? Do we have to trick like ListBoxItem doubleclick ?
Probably because the binding failed. Check the output window of VS and see if there are any binding errors. I suspect the LoadSimpleMoviePopupCommand property is not on your data item class (ie. your Media class).
had the same problem, try this
<Button Command="{Binding DataContext.YourCommand,RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"
it's quite normal, he can't find your command binding inside the listbox because you set something like
<DataTemplate DataType ...
in that listbox so he'll be looking for that binding inside the datatype and not the viewmodel (I assume :)

Resources