WPF binding within binding in XAML - c#

So I have a custom WPF UserControl called Row. It has a DependencyProperty called Index. The DataContext of Row is a class called ViewModel which contains an ObservableCollection.
Within the XAML of Row, if possible, I want to bind the text in a TextBox with whatever element is at Index inside said collection. So, something like this:
<TextBox Text="{Binding Elements[{Binding Index}]}" />
How can I access Index, which is in Row's codebehind, in Row's xaml if the DataContext of Row is something else?
I hope that I explained this clearly, thanks in advance for whatever help is provided.

Related

WPF Combobox - Displaying Count in Label

I've got a simple WPF ComboBox, displaying Orders/Positions on the Financial Markets.
<ComboBox Name="TradeDropDown"
HorizontalAlignment="Stretch"
VerticalAlignment="Top"
ItemsSource="{Binding Path=ActiveOrders}"
DisplayMemberPath="OrderLabel"
SelectedItem="{Binding Path=SelectedOrder, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" />
I need to see at a glance how many items are in the list. I've added a TextBlock above with summary information.
I don't like it, and would prefer to have the items in the dropdown listed like:
(1/2) Working Short 425K
(2/2) Filled Long 979K
etc - and have the 1/2 numbers correctly update as items are added and removed from the list.
The Items are stored in a BindingList.
Is there an easy way to do this?
Is there an easy way to do this?
Add another property to the class where the OrderLabel property is defined that returns a string like "(1/2) Working Short 425K" and set the DisplayMemberPath property of the ComboBox to the name of this property.
Make sure that the class implements the INotifyPropertyChanged interface.
You then set the new property to a new value and raise the PropertyChanged event whenever you want to update the label in the ComboBox.

StackPanel with User Controls in another User Control in Xaml Windows Phone

How Can I add child items to StackPanel in UserControl from C# code? Should I create something like DependencyProperty for it?
As it is easy to set Properties like Text for TextBlock in my UserControl, I have no idea how can I add items to StackPanel when using CodeBehind to do it dynamicaly.
StackPanel are only meant to be used for the most basic layout situations. It is far better using some kind of ListBox or ItemsControl depending on your requirements. You could add a collection DependencyProperty to your UserControl and do something like this:
In UserControl:
<ItemsControl ItemsSource="{Binding YourCollectionDependencyProperty, RelativeSource={
RelativeSource AncestorType={x:Type YourPrefix:YourUserControl}}}" ... />
Then you could data bind another collection property to the UserControl from outside the control:
Outside UserControl:
<YourPrefix:YourUserControl YourCollectionDependencyProperty="{Binding Items}" ... />
Then adding new items to be displayed in the UserControl is as simple as adding items to the Items collection:
Items.Add(someNewObject);
Please read the Data Binding Overview page on MSDN for further information on data binding.
Yes you can do something like this (this is just a sample):
TextBlock printTextBlock = new TextBlock();
printTextBlock.Text = "Hello, World!";
//MainStackPanel below is the name given to your control in xaml
MainStackPanel.Children.Add(printTextBlock);
Sources:
Programatically Adding Items to StackPanel

WPF ComboBox to ObservableCollection binding

I know there is a few topics on this problem but haven't find any solutions to this problem I have..
I have a ViewModel with an observable collection and I want to bind this collection to a combo box. However, there is no selected item, no index, just the collection itself.
in the XAML I have
ComboBox ItemsSource="{Binding OSCollection}" DisplayMemberPath="OSCollection.Name"
I believe the trouble lies with the bold above, I want to get a property from the collection called name, but like I say - no item will be selected before the bind.
I could use a foreach or something to extract the properties from the collection but I don't think this is the MVVM and WPF way.
Any help would be grateful.
Thanks
DisplayMemberPath specifies the path to the display property.So it should be Name not OSCollection.Name
ComboBox ItemsSource="{Binding OSCollection}" DisplayMemberPath="Name"
In addition to Sajeetharans comment:
When binding to a List of Type T, DisplayMemberPath will always refer to the Name of a Property of T. In your case it is only "Name"

TextBlock width binding to GridView

On my XAML page I have a text block with following binding:
<TextBlock Width="{Binding ActualWidth, ElementName=SessionList, Mode=OneWay}" ... />
This binds to a grid view:
<GridView x:Name="SessionList" ItemsSource="{Binding Sessions}"... />
Now when the page first loads and data is available, the text block will be visible and have the correct width. When the page loads and there is no data, the text box will not be visible because of the bound width.
But ... when I load up data in the background and after a while the data comes in (through MVVM) the list will be show, but the text block width will not change accordingly, and setting it as TwoWay has no effect.
Any ideas/tips?
ActualWidth is not a property that you can bind to within WinRT. Not sure if you are showing static text or bound text. If bound text and data is same as GridView has then it should go away if data is null. If static data, then use a ValueConverter to set the visibility of the TextBlock based on the data being null/empty
Binding issues like this are usually caused by properties that are not bindable, i.e. they are not dependency properties and/or do not implement INotifyPropertyChanged. Whatever. I use a Attached Dependency Property or, if that does not cover enough, a behavior. Now behavior are not included in WinRT, but that problem has already been addressed ;-)

Use Table without messing with DataGrid

Is it possible to make a table having cells bound to several objects (for example, textboxes) without making use of DataGrid?
Example:
<TextBox Text="{Binding Path=FileName}" Width="300"></TextBox>
The DataContext for the textbox's container should contain a Property named FileName
You should note that your property should be wired to notify when it is changed. See the following for more information:
http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

Categories