Why the need for Binding in this context - c#

i am starting WPF, looking at How do o: Getting started with Entity Framework
i am abit confused why the need for
<ListBox Name="ListBox1" ItemsSource="{Binding Source={StaticResource CustomerSource}}" >
why cant i do
<ListBox Name="ListBox1" ItemsSource="{StaticResource CustomerSource}" >
how do i know when i need Binding. because on 1st thought, just like i use a static resource in Styles
<Button Style="{StaticResource someStyle}"
why not
<Button Style="{Binding Source={StaticResource someStyle}}"

This example assigns a value retrieved from the resources using the specified key to the Text property:
<TextBox Text="{StaticResource SomeText}" />
This examples binds the Text property to a property on an object retrieved from the resources using the specified key:
<TextBox Text="{Binding Source={StaticResource SomeObject}, Path=SomeProperty}" />
The Binding class is used for data binding that is a way to surface data retrieved from a data source on the GUI, allowing users to interact with it. Without data binding values are simply assigned to the controls on the UI.
Bindings add a layer of abstraction between the UI controls and the underlying data source associated with it, providing a bounce of services. Here are some of the most important ones:
Automatic propagation of changes in the data between the UI and the data source in either or both directions
Conversion/formatting of values
Notification through events
Related resources:
Data Binding Overview
Binding Class

You could also set the DataContext on the parent control instead.
If the ListBox is contained in for example a StackPanel for example.
<StackPanel x:Name="parentControl" DatContext="{StaticResource CustomerSource}">
<ListBox x:Name="ListBox1" ItemSource="{Binding}">
...
</ListBox>
</StackPanel>

Related

Adding Filter TextBox to a ComboBox using MVVM in WPF

What I am trying to do is create a ComboBox where at the top there is a textbox that I can type into to filter the items within the ComboBox. Here is a an example of what I mean:
I need to do this using an MVVM approach. I am not sure how to go about this or how to overwrite the style to do so. I have looked on google for several solutions but none of them are quite exactly what I need. I am pretty sure once I have the style created I can figure out the filtering portion within my view model.
Any help would be appreciated.
Use IsTextSearchEnabled from the ComboBox control like this:
<ComboBox IsTextSearchEnabled="True" IsTextSearchCaseSensitive="True or False depending on your scenario" />
In my projects, when I do something like this, I add a TextBox as the first item in the dropdown content template, with a presenter following it of the items that need to be data-bound.
<ComboBox>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Path=FilteredText"} Mode="TwoWay"/>
<ListBox ItemSource="{Binding Path=ItemsForBinding}" Mode="TwoWay" NotifyOnSourceUpdated="True" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
And in your view-model, make sure that NotifyOnProperyChanged is enabled for the FilteredText property when it is updated, it will trigger the "removal" of the bound-items, I usually use ObservableCollection, but I know ListCollectionView has capabilities to filter and notifies the UI when the collection changes. You can even find a 3rd party text AutoCompleteBox ( I use Telerik,) and it would allow you to prepopulate the terms in the "textbox" that you want the user to be able to filter.

WP7 : access and change controls inside a ListBox DataTemplate based on the data from the database

I know this has been asked before but Im a bit confused !
I'm writing my app with MVVM so far so good. but now I need to know what is the best way to access my controls inside a data template in a listbox.
I want to access them through the code behind and also be able to change them based on the other values from the database !
Here is the view :
<ListBox Margin="0,8,0,0" toolkit:TiltEffect.IsTiltEnabled="True" x:Name="counterlist" ItemsSource="{Binding Groups}" HorizontalAlignment="Center" Tap="list_OnTap"
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="Orange" Width="125" Height="125" Margin="6">
<TextBlock Name="name" Foreground="White" Text="{Binding Name}" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap"/>
<TextBlock Name="items" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" TextAlignment="Center" TextWrapping="Wrap"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
This list box is bound to the Groups Observable collection in my viewModel. Again so far so good.
Now I have a method called
public int ItemsinGroup(int gid)
This method returns number of items in each group but its not simply based on this database and it also get some info from an external source so I cant simply make a query to add this also to the observable collection.
I just need to add this to each item in the list box so that it shows the associated item counts for each group.
I want to be able to change it in the code behind. I mean I want to access each loop of data in code behind as well as XAML (Which we are already doing it through binding) .
If I can do so it will be easy to inject ItemsinGroup results to its related item in the list box loop.
Can I do that by placing my method in viewModel ? but what about the current item in the loop, how can I find out what is the current ID of each Group in the listbox loop ?
I need to know what is the best way to do such things, what usually everyone does in these cases !
Thanks a lot
Your desire to use MVVM and yet update individual items in a collection from code behind seem contradictory.
This simplest solution is probably to get your collection of groups and before you bind them to the UI you loop through them and add the count to the objects in your collection.
Alternatively, if you want to bind the collection as quickly as possible and then update it. You could step through the collection once bound and as long as the objects in your collection implement INotifyPropertyChanged you could update them and have a second TextBlock in the ItemTemplate show this when set.

TextBlock Text-Binding from ListView items

How can I change the Text of a TextBlock when the selection in my ListView changes?
I don't want do this manually...
All Items of the ListView are LogEntry's (class)... Can I use Binding in the Text-Attribute of the TextBlock to get a specific property of the selected Item?
Yes, in fact there are multiple solutions, i give you the most "WPF" like answer, but imo also the least flexible.
First you need to set the IsSynchronizedWithCurrentItem="True" property
Now if you select an item, the bound CollectionView will set the item as the CurrentItem.
Now your TextBox/Block can bind to this specific item via a special binding syntax using a '/'.
For Example:
<TextBlock Text="{Binding LogEntries/}"/>
of course you can get a specific property from the current item via binding aswell
<TextBlock Text="{Binding LogEntries/WarningMessage}"/>
Hope that helps.
assuming you have a listview like this:
<ListView ItemSource="{Binding LogEntries}" Name="logs" IsSynchronizedWithCurrentItem="True">
</ListView>
<ContentControl Content="{Binding ElementName=logs, Path=SelectedItem}" ContentTemplate="{StaticResource logTemplate}"/>
Now you need to provide that logTemplate in the Resources.
<UserControl.Resources>
<DataTemplate DataType="{x:Type local:LogEntry}">
<TextBlock Text="{Binding Path=LogText}"/> <-- This is a Property-Binding of your custom class
</DataTemplate>
</UserControl.Resources>
The last thing missing is to provide the namespace to your local class LogEntry. If you use an awesome tool like Resharper, it will insert the namespace for you. Otherwise, here a sample declaration:
<UserControl xmlns:local="clr-namespace:My.App.Namespace.LogEntry;assembly=My.App"
... (rest of namespace declarations)

Collection View Source Grouping in Tree View - Binding Error

I have a TreeView databound to a CollectionViewSource Groups collection. This is so that I can display the data using the power of the CollectionViewSource, and the data itself also has a hierarchical structure, which is why I need the TreeView. I have a second control which is bound to the SelectedItem of the TreeView.
The problem is that when the group header is selected, the program crashes with the following exception.
{"A TwoWay or OneWayToSource binding cannot work on the read-only property 'Name' of type 'MS.Internal.Data.CollectionViewGroupInternal'."}
The objects in my TreeView contain a Name property that is two way bound in another control. The binding engine seems to find the Name property for the Group and attempt to bind to that. How can I prevent this exception from occuring? I would like for the rest of my program to treat it as if nothing is selected when the group header is selected, or disallow selecting the group header all together. Below is a simplified version of the code.
<TreeView
x:Name="CustomersTree"
ItemsSource="{Binding CustomersViewSource.Groups}"
ItemTemplate="{StaticResource CustomerGroupsTemplate}">
<MyUserControl DataContext="{Binding ElementName=CustomersTree, Path=SelectedItem, Mode=OneWay}" />
<HierarchicalDataTemplate x:Key="CustomerGroupsTemplate" ItemsSource="{Binding Path=Items}" ItemTemplate="{StaticResource CustomerTreeItemTemplate}">
<TextBlock Text="{Binding Path=Name}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="CustomerTreeItemTemplate" ItemsSource="{Binding Customers}">
<StackPanel>
<Image Source="{Binding ImageSource}" />
<TextBlock Text="{Binding Path=Name}" />
</StackPanel>
</HierarchicalDataTemplate>
To be clear, the error is a result of the binding in the CustomerGroupsTemplate as far as I can tell, and changing this binding to OneWay results in the same error. The information in the tree shows up the way it is expected to, it is only when a group header is selected that the exception occurs.
The problem was the result of a two way binding within the user control. I ended up using a converter to check for the type of the object being bound, ignoring it if it wasn't the object I wanted.

How to create a template to display data from a class in WPF

I have a data layer which is returning lists of classes containing data. I want to display this data in my form in WPF. The data is just properties on the class such as Class.ID, Class.Name, Class.Description (for the sake of example)
How can i create a custom control or template an existing control so that it can be given one of these classes and display its data in a data-bound fashion.
Thanks :)
You could use a ListBox and set its ItemsSource property to the list containing your data items. Then you define a DataTemplate for your type like this:
<DataTemplate x:Key="MyDataTemplate" DataType="{x:Type MyType}">
<StackPanel>
<TextBlock Text="{Binding ID}"/>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Description}"/>
</StackPanel>
</DataTemplate>
...and tell the ListBox to use this DataTemplate by setting the ItemTemplate property.
It is also sufficient to just define the DataTemplate as above and give it no key. Then it will be used for all items which have the respective type.
BTW: You can find a more detailed example in MSDN on the page for the ItemTemplate property.

Categories

Resources