Multi Silverlight databinding - c#

Does anyone know if its possible to do a binding and (if not how to achieve the same effect) on the same property using more than one binding in sort of a template
i.e.
A textblock that has Text bound in the expression
"{Binding Path=Contact.Title} {Binding Path=Contact.Firstname} {Binding Path=Contact.Surname}"
all in one text property

Not a big deal just:
<TextBlock>
<Run Text="{Binding Path=Contact.Title}"/>
<Run Text="{Binding Path=Contact.Firstname}"/>
<Run Text="{Binding Path=Contact.Surname}"/>
</TextBlock>

AFAIK it's not possible.
This is one of the reasons to follow the MVVM pattern, create an intermediary view which reflects the data in a format that you actually want presented, so you would create a fullname property on that class that was a concatenation of those fields and then bind to that.

Value Converters are one solution for binding to multiple values:
http://timheuer.com/blog/archive/2008/07/30/format-data-in-silverlight-databinding-valueconverter.aspx#11262
In that scenario you'd bind your TextBlock's Text property to the Contact object and specify a custom value converter that you've created. The converter can perform the string formatting based on property values.

I don't think it is possible to do it directly in xaml. I would absolutely love multiple bindings to one property.
What I have learned however, is that you can accomplish things similar to this using a couple different strategies:
Using a Stackpanel:
<StackPanel Orientation="Horizontal">
<TextBlock Text="Hello, "/>
<TextBlock Text="{Binding Contact.Title}"/>
<TextBlock Text="{Binding Contact.Firstname}"/>
<TextBlock Text="{Binding Contact.Surname}"/>
<TextBlock Text="!"/>
</StackPanel>
Using a Converter:
<TextBlock Text="{Binding Contact,
Converter={StaticResource ContactNameConverter}}"/>
More Info On Converters

Related

How to bind two elements to the same property in WPF - C#

I'm using C# in a WPF application with MVVM (with Caliburn Micro framework). I'm trying to bind 2 elements (one TextBlock and one TextBox) to the same property, that resides in my model view. My property is called FirstName.
I have two options to do the binding: Binding Path=FirstName or x:Name=FirstName. When I edit the textbox, I see the changes in the textblock only if I bind in a certain way (see code). Any idea of why the other way does not work? (when I type in the textbox I don't see my textblock updates)
I've tried different mode options (two ways, one way, etc). The NotifyOfPropertyChange seems to be working.
<!-- This works -->
<TextBlock Text="{Binding Path=FirstName}"/>
<TextBox x:Name="FirstName"/>
<!-- This does not work -->
<TextBlock x:Name="FirstName"/>
<TextBox Text="{Binding Path=FirstName, Mode=TwoWay}"/>
With your second example, you need to specify UpdateSourceTrigger=PropertyChanged:
<TextBlock x:Name="FirstName"/>
<TextBox Text="{Binding Path=FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
Otherwise, the source is only updated when the TextBox loses focus.

C# WPF textbox when binding data,how to add other strings in the Text property?

<TextBox x:Name="V2" Grid.Row="2" Grid.Column="8" Grid.ColumnSpan="3" Text="{Binding airPlane}".../>
In the above code, if I want to add more things in the "Text" property besides binding the airplane data, e.g. to add a unit called "Pounds", how to modify the code? Thanks!
You can use Converters for it. For more see WPF Converters

Any similar "ListView" UI Control in Xamarin.Mac?

I'm planning to design an multiple file downloader app (similar to IDM or Transmission) for macOS based on Aria2 JSON-RPC and C# GUI via Xamarin.Mac. But there is a major issue for UI design. I need a UI control which is similar to "ListView" in XAML.
Basically it's something like in this topic discussed, i.e. I need something equivalent in Xamarin.Mac with this XAML code below:
<ListView x:Name="DownloadItemList">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding DownloadItemTitle}"
Margin="20,0,20,8"
FontSize="24"
FontStyle="Italic"
FontWeight="SemiBold"
Foreground="DarkBlue" />
<TextBlock Text="{Binding DownloadProgressInfo}"
Margin="20,0,20,8"
FontSize="16"
Foreground="DarkGray"
Opacity="0.8" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
I also need some data bindings in the UI code if possible. But so far I can't find any similar stuff in Xamarin.Mac. Is there any possible solution like this? Thanks in advance!
You can use NSTableView
static Content :
You can design cells from storyboard, and if required can modify content of these cells at runtime.
Dynamic Content : You can crate template cell at design time (or progrmmatically id requried ), give it identifier and use that cell multiple-time in tableviewd atasource.
You can use something like a table view
Hope this helps.
Link

LongListSelector with two ItemTemplate

I use this code to add ListBox to my app:
<phone:LongListSelector x:Name="searchList" Margin="0,72,0,0" SelectionChanged="DidPressSelectSearchList">
<phone:LongListSelector.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,20,0,0">
<TextBlock Text="{Binding}" FontSize="25" />
</StackPanel>
</DataTemplate>
</phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
Now my issue is that i want to use two kind of ItemTemplate, because there is two ways i show data to the user:
1) Array of Strings
2) Array of objects(2 Strings)
Any help how i can use the list to show two kind of objects?
The easiest way to do this in WP7 is with a Template Selector.
Like this one
Though I cant test it right now, WP8 should support the DataType property on the DataTemplate class, which means you can define implicit Data Templates for each data type and skip the selector altogether.

How do I hide a cell in a RadGridView depending on the current object's bool property?

I currently have a list of objects in which my RadGridView's ItemsSource is set to. When the property "DoNotContact" of the object in the list has been set to True, I want to hide the information in the cell that contains a Phone number within my RadGridView. As you can see in my XAML, I'm setting the Visibility property within the TextBlock like so:
<telerik:GridViewDataColumn Header="Evening" DataMemberBinding="{Binding Path=EveningPhone}" Width="75" SortMemberPath="EveningPhone">
<telerik:GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Visibility="{Binding Path=DoNotContact, Converter={StaticResource BoolToVisibilityConverter}}">
<Hyperlink Click="MakeEveningCallHandler">
<TextBlock Text="{Binding Path=EveningPhone}" />
</Hyperlink>
</TextBlock>
</DataTemplate>
</telerik:GridViewColumn.CellTemplate>
</telerik:GridViewDataColumn>
When attempting to debug it, the Converter is never hit and although I can see the property "DoNotContact" has been set, the phone number still shows. The converter itself works fine as I've used it in other occasions. Again I only want to hide the information WITHIN the cell for the "Evening" Property, not the actual column itself. Any Ideas what's going wrong here? Thanks a bunch!
The code you provided works for me!

Categories