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?
Related
So i have this Image inside GridViewColumn:
<Image Width="16"
Height="16"
Source="{Binding IsChecked, Converter={StaticResource myConverter}}"/>
So IsChecked is my model property and base on its value i am changing my Image Source.
So Until here all works fine.
Now i want to change my Image Source also if MouseOver over my ListViewItem and i this case the imgae source will be different if MouseOver is true or false.
Any seggustion how to do that ?
I was thinking of use IMultiValueConverter but how can i pass into my converter the ListViewItem MouseOver value ?
A MultiBinding like this should work:
<Image>
<Image.Source>
<MultiBinding Converter="{StaticResource myConverter}">
<Binding Path="IsChecked"/>
<Binding Path="IsMouseOver" RelativeSource="{RelativeSource Self}"/>
</MultiBinding>
</Image.Source>
</Image>
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>
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>
I am trying to set the IsEnabled property of a button using multibinding, because this property depends on three variables.
If I would set the content propery, I could use this code:
<Button Height="23" HorizontalAlignment="Left" Margin="629,49,0,0" Name="btnMyButton" VerticalAlignment="Top" Width="75">
<Button.Content>
<MultiBinding Converter="{StaticResource myMultiValueConverter}">
<Binding ElementName="MyElement"/>
<Binding />
</MultiBinding>
</Button.Content>
</Button>
I try to use this code:
<Button Height="23" HorizontalAlignment="Left" Margin="629,49,0,0" Name="btnMyButton" VerticalAlignment="Top" Width="75">
<Button.IsEnabled>
<?????
But in this case, although the Button.IsEnabled is avaliable, in the next line I can't find the Multibinding keyword, so I can't use multibinding with the IsEnabled property.
why? Is there any way to set the IsEnabled property with a multivalue converter?
Thanks.
The syntax should be the exact same as what you have for Button.Content - just replace "Content" with "IsEnabled".
<Button.IsEnabled>
<MultiBinding Converter="{StaticResource myMultiValueConverter}">
<Binding ... />
<Binding ... />
<Binding ... />
</MultiBinding>
</Button.IsEnabled>
It might not auto-complete for you because the IsEnabled property expects a boolean value, not a MultiBinding object, but it shouldn't give you any errors, and will compile and execute just fine.
(It auto-completes for Button.Content because the Content property is of type object, which includes a MultiBinding object)
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 :)