WPF Commands Buttons ListBoxItems - wpf

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 :)

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>

How can I make a control in Wpf depend on a property in viewmodel?

I have this codes, but I don't want just make control visible or invisible , I don't want to create it, because it works in background and does some work that takes time and I don't want it. how can I do that? Can I use if condition in xaml in wpf? I've heard about triggers but I don't know how to do that in my example?
<ContentControl Content="{Binding ViewService[NoteSaveView]}" Visibility="{Binding ViewModeVisibility[Insert]}"/>
<telerik:RadTabControl TabOrientation="Horizontal" TabStripPlacement="Right" Padding="5" BorderThickness="0" Margin="0 -5 -5 -5" Visibility="{Binding ViewMode,Converter={StaticResource UpdateViewToVisibility}}">
<telerik:RadTabItem Header="صفحه اصلی" Content="{Binding ViewService[NoteSaveView]}"/>
<telerik:RadTabItem Header="تاریخچه" Content="{Binding ViewService[LogView]}"/>
</telerik:RadTabControl>
Edit:
finally I solved my problem with this code:
<ContentControl>
<ContentControl.Content>
<MultiBinding Converter="{StaticResource ViewModeVisibilityToContent}">
<Binding Path="ViewModeVisibility[Insert]"/>
<Binding Path="ViewService[NoteSaveView]"/>
</MultiBinding>
</ContentControl.Content>
</ContentControl>
You could use a DataType property of a DataTemplate and then assign a data object to the property to activate a DataTemplate or null to deactivate it.
In the code below you just need to set PropertyInViewModel of type object in viewmodel to some int to activate the DataTemplate, or to null to deactivate it.
<StackPanel xmlns:System="clr-namespace:System;assembly=mscorlib">
<StackPanel.Resources>
<DataTemplate DataType="{x:Type System:Int32}">
<TextBox Text="{Binding Mode=OneWay}" Foreground="Beige" Background="Brown" Height="20" Width="50"/>
</DataTemplate>
</StackPanel.Resources>
<ContentControl Content="{Binding PropertyInViewModel}"/>
</StackPanel>

WPF MVVM How read property value, binded to another element, in viewmodel

I'm trying to do a simple thing, I've searching around the web but I've not found a solution. I use ColorPicker from Wpf Toolkit library, the property SelectedColor is binded to an element Label on Foreground Property.
It works correctly, when I change color from color picker the label changes its color, but I can't find a way to get the selected color in my viewmodel.
Normally I used to bind my property to viewmodel and manage it on onPropertyChanged event, but how can I do in this situation?
XAML
<Label Name="LabelMeasureValue1" Content="{Binding TrendingMeasure1Value}" Style="{StaticResource LabelStyleBold}" VerticalAlignment="Center" HorizontalAlignment="Right" Grid.Row="3" Grid.Column="2"/>
<wpfTool:ColorPicker Name="ColorPick1" DisplayColorAndName="True" ShowStandardColors="False" AvailableColors="{Binding ColorItems}" SelectedColor="{Binding ElementName=LabelMeasureValue1, Path=Foreground, Converter={StaticResource brushColorConv}}" Tag="{Binding Path=SelectedItem, ElementName=ComboBoxMeasure1}" Grid.Row="3" Grid.Column="1" VerticalAlignment="Center" Height="30">
<wpfTool:ColorPicker.IsEnabled>
<MultiBinding Converter="{StaticResource enblTrendConv}">
<Binding Path="Device.EsitoUltimaLettura" />
<Binding Path="IsGraphRunning" Converter="{StaticResource invBoolValueConv}"/>
</MultiBinding>
</wpfTool:ColorPicker.IsEnabled>
</wpfTool:ColorPicker>

WPF ComboBox IsEditable at 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

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!

Resources