I need to databind into a element inside my custom class. i've given the ItemSource as the ObservableCollection of telerik:RadTransitionControl via an attached property. However, I need to provide the image member as the source to Image control. I tried the following method and was unsuccessful.
<Grid Background="Black">
<telerik:RadTransitionControl x:Name="radControl" adRotator:AdRotatorExtensions.ItemChangeDelay="0:0:3"
adRotator:AdRotatorExtensions.CurrentSelectedIndex="0"
adRotator:AdRotatorExtensions.IndexChanged="{Binding TopItemCommand, Mode=OneWay}"
adRotator:AdRotatorExtensions.ItemsSource="{Binding Path=ImagePaths}"
VerticalAlignment="Center"
HorizontalAlignment="Center" Width="650">
<telerik:RadTransitionControl.Transition>
<telerik:MotionBlurredZoomTransition />
</telerik:RadTransitionControl.Transition>
<telerik:RadTransitionControl.ContentTemplate>
<DataTemplate>
<Image Source="{Binding Path=ImagePaths.AdImage}" />
</DataTemplate>
</telerik:RadTransitionControl.ContentTemplate>
</telerik:RadTransitionControl>
</Grid>
an ImagePaths object is already set as the DataContext for the item. this means that the binding is already pointing (so to speak) at an instance of the object. so, when you tell it to bind on ImagePaths.AdImage it does not know how to find the property you are looking for. Good news is, all you have to do is provide the path on the object-- remove the ImagePaths part (and the dot) and you should be good to go.
for example...
class something
{
public string someImage {...}
}
<DataTemplate> <!--------- the data context of this item is an instance of
my "something" class, so i need to set the path
to be the property on the object --->
<Image Source="{Binding Path=someImage}" />
</DataTemplate>
here is a very helpful article on debugging bindings in WPF
for more info here is an excellent article on MSDN
here is a datatemplate article from Dr. WPF
Related
I'm trying to use the PropertyGrid component from PropertyTools to display information on an object. I can bind the object easily enough -- it's a property on my DataContext -- but one of the things that can't be derived from the object is the name that should be displayed in the tab header. (And I can't change that; the object I'm inspecting comes from a third party.) The proper name is a different property on my DataContext.
PropertyGrid has a way to change the way the tab header is displayed, by passing a DataTemplate to its TabHeaderTemplate property. But something bizarre happens inside of the template: my DataContext is gone, replaced by something else. When I try to say {Binding TabName} in the appropriate place inside the context, it errors out and tells me that TabName is not a valid property on class Tab. But my DataContext class isn't called Tab; that's something inside of PropertyTools's codebase!
I'm still new to WPF, so I have no clue what's going on here. Why is the in-scope DataContext that's perfectly valid in the rest of the XAML file being yoinked out from under me inside this template, and how can I fix it?
EDIT: Posting the XAML as requested. The template is literally just the simplest possible thing:
<UserControl.Resources>
<DataTemplate x:Key="HeaderTemplate">
<TextBlock Text="{Binding TabName}" />
</DataTemplate>
</UserControl.Resources>
And then further down the page,
<props:PropertyGrid
SelectedObject="{Binding Value}"
TabHeaderTemplate="{StaticResource HeaderTemplate}" />
But for some bizarre reason, in the template it's trying to interpret the binding inside the wrong DataContext!
In this case, just be sure to specify the source in your binding. There are a few ways to do this. One is to use the RelativeSource property of the Binding. Another is to use ElementName
Give your UserControl this attribute:
x:Name="Root".
Then change your binding to use it
<TextBlock Text="{Binding ElementName=Root, Path=DataContext.TabName}" />
Or use this:
<TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MyUserControl}}, Path=DataContext.TabName}"/>
So, I made a really simple attempt to try out data binding from a property of a class that I have, but, for whatever reason, the code actually do anything. It's not throwing any errors, but something must not be working right. I'm just currently testing if it'll behave like I want it to, which, in this case, will set the opacity of a rectangle to zero. Here's the xaml for the Data Template that doesn't seem to want to respond correctly:
<HubSection x:Name="China" Width="440" Height="460" Background="#FF343434" Header="China" IsHeaderInteractive="True" Tapped="{x:Bind HubSectionTapped}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="50,0,0,0">
<DataTemplate x:DataType="data:MainPageView">
<Grid Height="460" Width="410" VerticalAlignment="Bottom" x:Name="ChinaBackground">
<Image Source="Assets/chinaFlag.bmp" x:Name="ChinaFlag"/>
<Rectangle x:Name="ChinaSelected_Rect" Width="410" Height="30" VerticalAlignment="Bottom" Fill="BlueViolet" Opacity="{x:Bind Opacity1}"/>
</Grid>
</DataTemplate>
</HubSection>
And here's the code behind:
public MainPageView TheMainPageView;
public MainPage()
{
this.InitializeComponent();
timer = new DispatcherTimer();
timer.Tick += Timer_DyanmicResize;
timer.Tick += Timer_SelectionIndicator;
timer.Start();
TheMainPageView = new MainPageView ();
}
And finally, here's the class MainPageView that's referenced:
public class MainPageView
{
public int Opacity1 {get; set;}
public int Opacity2 {get;set;}
public int Opacity3 { get; set; }
public MainPageView()
{
this.Opacity1 = 0;
this.Opacity2 = 0;
this.Opacity3 = 0;
}
}
In the XAML I included the xmlns:data="using:TestApp.Models" (models is the folder in which the class MainPageView is housed). As I said, it's not throwing errors, but it's not doing anything either, so I'm a bit at a loss of where to start addressing this because there aren't any errors to trace back. Thanks in advance for any help you guys can provide
HubSection uses a DataTemplate to define the content for the section, content can be defined inline, or bound to a data source. When using binding in this DataTemplate, we need set DataContext property of HubSection to provide data source for the DataTemplate.
{x:Bind} does not use the DataContext as a default source—instead, it uses the page or user control itself. So it will look in the code-behind of your page or user control for properties, fields, and methods.
This is right when you use {x:Bind} directly in page or user control. While Inside a DataTemplate, there is a little difference.
Inside a DataTemplate (whether used as an item template, a content template, or a header template), the value of Path is not interpreted in the context of the page, but in the context of the data object being templated. So that its bindings can be validated (and efficient code generated for them) at compile-time, a DataTemplate needs to declare the type of its data object using x:DataType.
For more information about Data binding in UWP, please check Data binding in depth.
To fix your issue, you just need to set DataContext in HubSection like following:
<HubSection x:Name="China" Width="440" Height="460" Background="#FF343434" Header="China" IsHeaderInteractive="True" Tapped="{x:Bind HubSectionTapped}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="50,0,0,0" DataContext="{x:Bind TheMainPageView}">
<DataTemplate x:DataType="data:MainPageView">
<Grid Height="460" Width="410" VerticalAlignment="Bottom" x:Name="ChinaBackground">
<Image Source="Assets/chinaFlag.bmp" x:Name="ChinaFlag"/>
<Rectangle x:Name="ChinaSelected_Rect" Width="410" Height="30" VerticalAlignment="Bottom" Fill="BlueViolet" Opacity="{x:Bind Opacity1}"/>
</Grid>
</DataTemplate>
</HubSection>
Here when using {x:Bind} in HubSection, it uses the page itself as its data source as HubSection is in the page directly. So it can get TheMainPageView field in the code-behind. But for the {x:Bind} in DataTemplate, it can't as
its data source is the data object being templated not the page. So we need to provide this data object by setting DataContext property of HubSection.
Check you output window for errors but I imagine you might see a binding error in there. Opacity is a double, you are using an int so will get a type conversion error.
I have a little question.
I have a data template like that :
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="DangersItemTemplate">
<Grid Height="102" Width="447" Tap="Grid_Tap">
<Image Name="image" Source="{Binding Image}" HorizontalAlignment="Left" Width="90" />
<TextBlock Name="text" Text="{Binding Nom}" TextWrapping="Wrap" Margin="102,16,16,22"/>
</Grid>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
I want to use the Textlock control in the CSharp code to pass the Text property to the second xaml page, but I can't find the name of the control, it's inaccessible.
Any idea ?
Your binding the Text of the TextBlock so you must have the value in your datacontext. I'd add this value to the Querystirng and navigate to the page.
var text = ((Whatever) DataContext).Nom;
string page2Uri = string.Foramt("/PAge2.xaml?Nom={0}", text);
NavigationService.Navigate(new Uri(page2Uri, UriKind.Relative));
And then to get the data out of the querystring you can use the OnNavigatedTo method in your page, and then inspect this.NavigationContext.QueryString["nom"]
Or have a look into something like the MVVM pattern.
I just use VB.Net but I am sure you can convert.
Just use any shared variable in the application.
Here you can see it in an SourceCode example:
http://www.activevb.de/rubriken/kolumne/kol_30/res/nachtscanner.zip
Just use Public Shared MyText As String
in Application Class. This vairable you can access anywhere.
You can also use INotifyPropertyChanged for that shared property to bind.
Alternatively you can use Navigation Query. Read more here:
How can I pass query string variables with NavigationService.Navigate?
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>
I used Microsoft's Chart Control of the WPF toolkit to write my own chart control.
I blogged about it here. My Chart control stacks the yaxes in the chart on top of each other. As you can read in the article this all works quite well. Now I want to create a viewmodel that controls the data and axes in the chart. So far I'm able to add axes to the chart and show them in the chart. But I have a problem when I try to add the lineseries because it has one DependentAxis and one InDependentAxis property. I don't know how to assign the proper xAxis and yAxis controls to it.
Below you see part of the LineSeriesViewModel. It has a nested XAxisViewModel and YAxisViewModel property.
public class LineSeriesViewModel : ViewModelBase, IChartComponent
{
XAxisViewModel _xAxis;
public XAxisViewModel XAxis
{
get { return _xAxis; }
set
{
_xAxis = value;
RaisePropertyChanged(() => XAxis);
}
}
//The YAxis Property look the same
}
The viewmodels all have their own datatemplate.
The xaml code looks like this:
<UserControl.Resources>
<DataTemplate x:Key="xAxisTemplate" DataType="{x:Type l:YAxisViewModel}">
<chart:LinearAxis x:Name="yAxis" Orientation="Y" Location="Left" Minimum="0" Maximum="10" IsHitTestVisible="False" Width="50" />
</DataTemplate>
<DataTemplate x:Key="yAxisTemplate" DataType="{x:Type l:XAxisViewModel}">
<chart:LinearAxis x:Name="xAxis" Orientation="X" Location="Bottom" Minimum="0" Maximum="100" IsHitTestVisible="False" Height="50" />
</DataTemplate>
<DataTemplate DataType="{x:Type l:LineSeriesViewModel}">
<!--Binding doesn't work on the Dependent and IndependentAxis! -->
<!--YAxis XAxis and Series are properties of the LineSeriesViewModel -->
<l:FastLineSeries DependentAxis="{Binding Path=YAxis}"
IndependentAxis="{Binding Path=XAxis}"
ItemsSource="{Binding Path=Series}"/>
</DataTemplate>
<Style TargetType="ItemsControl">
<Setter Property="ItemsPanel">
<Setter.Value>
<ItemsPanelTemplate>
<!--My stacked chart control -->
<l:StackedPanel x:Name="stackedPanel" Width="Auto" Height="Auto" Background="LightBlue">
</l:StackedPanel>
</ItemsPanelTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ClipToBounds="True">
<!-- View is an ObservableCollection of all axes and series-->
<ItemsControl x:Name="chartItems" ItemsSource="{Binding Path=View}" Focusable="False">
</ItemsControl>
</Grid>
This code works quite well. When I add axes they get drawn. But the DependentAxis and InDependentAxis of the lineseries control stay null, so the series doesn't get drawn. How can I bind the nested viewmodels to the properties of a control?
It should work. A few things you can check:
Does the Series Binding work? If so, try to figure out what's the difference.
Are you sure that the XAxis and YAxis properties actually have values? Try putting a breakpoint in the getter. If it's reached, the Binding works. You can also put a converter (IValueConverter) on the Binding (that simply returns the value it receives) and place a breakpoint there.
Use PresentationTraceSources.TraceLevel=High on the Binding to get more verbose tracing (that will appear in the VS Output window).
Are DependentAxis/IndependentAxis defined as dependency properties on FastLineSeries?
Hope that helps,
Aelij.
You've probably already checked this but I find that when I'm debugging bindings the first and easiest place to start is running a debug session from VS as the debug output tells which objects and properties are failing to bind. I usually end up discovering I need to explicitly set a DataContext or something else like a typo. The output to look for start like this:
System.Windows.Data Error: 39 :
BindingExpression path error:
this is followed by the property name you tried to bind to and usually most importantly the class against which its actually trying to bind. If this doesn't help there's a great article here on the debugging bindings: http://www.beacosta.com/blog/?p=52 which discusses the use of PresentationTraceSources.TraceLevel=High which Aelij mentioned, as well as a few other techniques. Hope this gets onto the right track.
Regards,
Mike