Binding with an anonymous object on WP7.5 - c#

On WP7.5 I created a datacontext like this:
this.DataContext = new { One = listOne, Two = listTwo};
On my XAML code I tried this:
<TextBlock Text="{Binding listOne.m_strTypeTiers}" Style="{StaticResource PhoneTextNormalStyle}" />
<TextBlock Text="{Binding listTwo.m_strTypeTiers}" Style="{StaticResource PhoneTextNormalStyle}" />
My textbox are empty. I think my binding syntax has a mistake.

I think you are looking for {Binding One.m_strTypeTiers}
But it depends on what listOne and listTwo are. Are they classes with a property named m_strTypeTiers? If m_strTypeTiers is a field, then you will not be able to databind to it. Databinding only works with properties (by default).
UPDATE
Oops. Silverlight does not support binding to anonymous types. I was thinking of WPF for desktop applications. Windows Phone 7 uses Silverlight which is very similar but different in some important ways. I think you are out of luck - you will need to define a concrete class.
You may find this article helpful in the future:
Contrasting Silverlight and WPF (...not that it would have helped you with this particular question)

Related

WPF MVVM Chartplotter: Dynamically bind multiple Lines

I'm trying to make the WPF Chartplotter work with some WPF MVVM. As a target of my interest I've chosen our TeamCity Tests Performance. The current Plotter is looking like this:
<dd:ChartPlotter Grid.Row="1">
<dd:ChartPlotter.HorizontalAxis>
<dd:HorizontalIntegerAxis />
</dd:ChartPlotter.HorizontalAxis>
<dd:ChartPlotter.VerticalAxis>
<dd:VerticalIntegerAxis/>
</dd:ChartPlotter.VerticalAxis>
<dd:VerticalAxisTitle Content="Performance in Minutes" FontSize="10" />
<dd:HorizontalAxisTitle Content="Build by ID" FontSize="10" />
<dd:LineGraph DataSource="{Binding TestRunsPerformance, IsAsync=True}" Stroke="Green" StrokeThickness="2">
<dd:LineGraph.Description>
<dd:PenDescription DescriptionString="Test" />
</dd:LineGraph.Description>
</dd:LineGraph>
<dd:LineGraph DataSource="{Binding TestSlowRunsPerformance, IsAsync=True}" Stroke="Red" StrokeThickness="2">
<dd:LineGraph.Description>
<dd:PenDescription DescriptionString="TestSlow" />
</dd:LineGraph.Description>
</dd:LineGraph>
</dd:ChartPlotter>
</Grid>
So I have basically two Test-Categories and for each I show a LineGraph in the Plotter, which I bind to the EnumerableDataSource in the ViewModel. Unfortunately I couldn't test it, but I'm certain it will work this way, since one LineGraph did work so far.
This is cool, but now I would like to make a second Chart with the Unit-Tests with the highest influence. But for this, I can't just create LineGraphs in the XAML and bind, but I would need something like a LineGraph Collection and set an amount of LineGraphs dynamically. Since I'm also learning MVVM, it would be troublesome If I'd really need to do this in the Code behind.
Unfortunately, all the Examples I found so far do exactly this, for example: Dynamic Line chart in C# WPF Application
I found also examples to make this work for Canvas: Is it possible to bind a Canvas's Children property in XAML? but these are UIElements, not PlotterElements.
Is what I'm trying even possible or am I doing something fundamentally wrong?

Items collection must be empty before using ItemsSource. Telerik Treeview

Im getting this error while trying to giva my treeview an itemsource
"Items collection must be empty before using ItemsSource."
I have checked a lot of solutions and I cant seem to find a way to solve this. Here are my code snippets:
XAML:
<HierarchicalDataTemplate x:Key="Category">
<TextBlock Text="{Binding Name}">
</TextBlock>
</HierarchicalDataTemplate>
XAML
<telerik:RadTreeView x:Name="treeview" IsDragDropEnabled="True" HorizontalAlignment="Left" Height="250" Margin="10,10,0,-3" VerticalAlignment="Top" Width="190" IsManipulationEnabled="True" IsLoadOnDemandEnabled="True" LoadOnDemand="treeview_LoadOnDemand" IsExpandOnSingleClickEnabled="True" ItemTemplate="{StaticResource Category}">
</telerik:RadTreeView>
C# - Giving the treeview a data source:
Data d = new Data();
treeview.ItemsSource = d.Get_Categories();
C# - My database query:
public List<Category> Get_Categories()
{
using (var context = new ProcessDatabaseEntities())
{
return context.Category.ToList();
}
}
Category only has two properties, Name and ID. I know that the itemsource-list is not empty when I assign it. So it's probably something wrong with my XAML-code. Thank you in advance!
I believe that your problem is a common one. Basically, you cannot use both the TreeView.ItemsSource and the TreeView.Items properties together... you must choose one way or the other. Usually this problem manifests itself because a developer has done something like this...:
<TreeView Name="TreeView" ItemsSource="{Binding SomeCollection}" ... />
... and then tried to do something like this in the code behind:
TreeView.Items.Add(someItem);
The solution in that case would be to manipulate the data bound collection instead of the TreeView.Items collection:
SomeCollection.Add(someItem);
However, in your case (and it's a little bit difficult to guess without seeing your code), you have probably done the second part first (set or manipulated the Items property) and then tried to set the ItemsSource property. Your solution is the same... use one method of editing the items or the other... not both.

Why is TextElement.FontSize an attached property in StackPanel in WPF

I am reading WPF 4 Unleashed and I am obviously new to WPF (and C# & .NET in general)
In the book, found the following snipped of code:
<StackPanel TextElement.FontSize="30" TextElement.FontStyle="Italic"
Orientation="Horizontal" HorizontalAlignment="Center">
<Button MinWidth="75" Margin="10">Help</Button>
<Button MinWidth="75" Margin="10">OK</Button>
</StackPanel>
TextElement.FontSize is an attached property. I don't understand why is it attached property ?
(Though, I do understand the concept of dependency property)
In Attached Properties Overview on MS site, there is another snipped of code.
<DockPanel>
<CheckBox DockPanel.Dock="Top">Hello</CheckBox>
</DockPanel>
In this case, it makes sense as to why DockPanel.Dock is an attached property - DockPanel class contains dependency property DockProperty.
If you're new to C#, I strongly suggest you start by doing some Hello, World! type of stuff in console applications before trying to get into complex WPF GUI stuff.
WPF is a complex framework not really suitable for the unexperienced. You must have a strong background in C# and OOP in general in order to learn MVVM, which is what you must learn in order to use WPF properly.
That said, StackPanel does not have a FontSize property because it's a Panel and not a Control, which is where the FontXXX properties are defined. That's why you can optionally define the TextElement.FontSize Attached Property, which are child controls will inherit due to Dependency Property Value Inheritance in the Visual Tree.
As an aside, that book was in a former coworker's desk, so I grabbed it and rapidly browsed thru it. I didn't find a single mention to MVVM, which at this point I consider a fundamental part of the WPF learning curve.

Passing values to DataTemplates with attached properties

I'm utilizing a data grid which has three columns of type DataGridTemplateColumn. These columns share almost identical behaviors and, as a consequence, utilize almost identical templates. The templates are copy-paste with a few resources changed out.
I would like to refactor the templates in to a generic version which uses an attached property to provide the necessary data. I've tried this but have been unable to access the property from inside the CellTemplate.
Methods I've tried are:
Bindings using RelativeSource: TemplatedParent.
Bindings using RelativeSource with various AncestorTypes.
Adding FrameworkPropertyMetadataOptions.Inherits to the FrameworkPropertyMetadata of the attached property.
The CellTemplate seems to have an odd degree of separation from its surroundings. What am I missing. If nothing, what is the appropriate solution to this problem?
A code example from you would have been helpful, but assuming that you have attached your property to the DataGrid, you should be able to bind to it from the CellTemplate using the following (untested):
<TextBlock Text="{Binding Path=(YourNamespace:YourClassName.YourAttachedPropertyName),
RelativeSource={RelativeSource AncestorType=DataGrid}, FallbackValue=''}" />
If you attached the property to the DataGridTemplateColumn, you would use:
<TextBlock Text="{Binding Path=(YourNamespace:YourClassName.YourAttachedPropertyName),
RelativeSource={RelativeSource AncestorType=DataGridTemplateColumn}, FallbackValue=''}" />
The FallbackValue property is not required, but it's a good practice to use it to avoid binding errors if the binding source can't be found.

Expose property in Windows Phone 7 custom ControlTemplate

I'm using Visual Studio 2010 & Expression Blend 4, the target is Windows Phone 7 platform.
What I would like to make is a custom control (specifically a custom PushPin for Map, but could be anything) and expose some of it's properties so I could change them. I'll try to explain better with an example:
<ControlTemplate x:Key="PushpinControlTemplate1" TargetType="Microsoft_Phone_Controls_Maps:Pushpin">
<Border BorderBrush="#FF0012AD" BorderThickness="3" Background="#FF0012AD" Width="32" Height="32" CornerRadius="5">
<TextBlock Text="2" FontWeight="Bold" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="32" Margin="0,-4,0,0"/>
</Border>
</ControlTemplate>
This example is from MSDN example.
Now, when I "use" this template on a map, it shows as expected, but with one problem. I would like that the Text property of the TextBlock (in this case... for example it could be ImageSource if I put an image into the PushPin) could be changed in the properties panel of Expression Blend, and in the C# code "behind" the XAML - of course, for each "instance" of this PushPin separately.
As far as I know, it has to do something with Dependency properties (I could be wrong?), but I am yet to find a clear example showing exactly WHAT, WHERE (C# / XAML) and WHY (sorry, I had to emphasize) had to be done.
I grasped most of the Phone 7 "topics" but now I'm a little stuck regarding resources and data bindings, as shown here :)
If someone could provide some sample code or a link to a good tutorial I would be grateful. Thanks!
If you want to simply set a user control property by code, you can use a normal property. If however you want to bind to the property, you must use a dependency property.
Good example here

Categories

Resources