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

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>

Related

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 Binding vs MultiBinding update

I had an issue that has been resolved.
WPF binding HorizontalAlignment to TextAlignment
I have a ListBoxItem whose HorizontalAlignment property is changed by a Converter, according to the width of the Window. I have a TextBlock inside this ListBoxItem whose TextAlignment property is binded to the HorizontalAlignment property of the ListBoxItem.
First, I used a Simple Binding, that didn't work (the HorizontalAlignment of the ListBoxIten changed, but the TextAlignment was not updated). Here is the code:
<ListBox>
<ListBoxItem>
<TextBlock TextWrapping="Wrap" Text="Some text"
TextAlignment="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}, Path=HorizontalAlignment}"/>
</ListBoxItem>
</ListBox>
After, I used a MultiBinding instead of a Binding, passing AcualWidth property, which actually is not used by the Converter. Now the TextAlignment is updated when ListBoxItem's HorizontalAlignment changes. I used the following code:
<TextBlock TextWrapping="Wrap" Text="Some text">
<TextBlock.TextAlignment>
<MultiBinding Converter="{StaticResource HorizontalToTextAlignmentConverter}">
<Binding Path="ActualWidth" RelativeSource="{RelativeSource Self}"/>
<Binding Path="HorizontalAlignment" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}"/>
</MultiBinding>
</TextBlock.TextAlignment>
</TextBlock>
The Converter itself is the same, it just has one more variable (ActualWidth of the Window) that is not used. So, my question is: Why the TextAlignment was update only with a MultiBinding?

Bind ObservableCollection count to wpf label and format the label

I have a label where I want to show the number of selected items in a list ( list items are selected from a grid ). That all works well and the label displays the number. What I want is to make the label display "5 items selected". Right now, I just get the number 5. Here is the xaml:
<Label Height="23" HorizontalAlignment="Left" Margin="7,2,0,0" Name="lblSelectionSummary" VerticalAlignment="Top" Width="557" FontFamily="Arial" >
<Label.Content>
<Binding Path="SelectedRows.Count" />
</Label.Content>
</Label>
I'm close on this guy.
you just need to specify the StringFormat in your binding
this should do
<Binding Path="SelectedRows.Count" StringFormat="{}{0} items selected"/>
above may not work for Label as it follows Content model , so you may need to use TextBlock instead
sample
<TextBlock Height="23" HorizontalAlignment="Left" Margin="7,2,0,0" Name="lblSelectionSummary" VerticalAlignment="Top" Width="557" FontFamily="Arial" >
<TextBlock.Text>
<Binding Path="SelectedRows.Count"
StringFormat="{}{0} items selected"/>
</TextBlock.Text>
</TextBlock>
or
<TextBlock Height="23"
HorizontalAlignment="Left"
Margin="7,2,0,0"
Name="lblSelectionSummary"
VerticalAlignment="Top"
Width="557"
FontFamily="Arial"
Text="{Binding SelectedRows.Count, StringFormat={}{0} items selected}" />
Use string format with a Label
StringFormat in binding works for string type properties, and since type of Content property of Label is object so StringFormat does not work
thanks to blindmeis for the hint
since a Label follows Content model it uses ContentStringFormat to format the value, below is an example to use the same
<Label Content="{Binding SelectedRows.Count}"
ContentStringFormat="{}{0} items selected" />

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

XAML - Binding to DataContext and using converter?

To bind to the current DataContext in XAML you can use:
<TextBlock Text="{Binding}" />
How do you do this using a converter in the mix?
The following works when you have a property on the path:
<TextBlock Text="{Binding MyProperty,Converter={StaticResource converter}}" />
But I dont want to do that; I just want to Bind to the datacontext and not the datacontext.MyProperty if you get what I mean.
Simply omit the path:
<TextBlock Text="{Binding Converter={StaticResource converter}}" />
Ah wait - I notice your question is tagged with Silverlight. Does this not work in Silverlight? If not, you may need to use the expanded syntax:
<TextBlock>
<TextBlock.Text>
<Binding Converter="{StaticResource converter}" />
</TextBlock.Text>
</TextBlock>
Dot sign also provide DataContext Binding for SL developers
<TextBlock Text="{Binding Path=.,Converter={StaticResource converter}}" />

Resources