User control data binding winrt - xaml

I am facing problem of data binding in user control.
<UserControl.Resources>
<CollectionViewSource
x:Name="itemsViewSource"
Source="{Binding Items}"
d:Source="{Binding AllGroups[0].Items, Source={d:DesignInstance Type=data:SampleDataSource, IsDesignTimeCreatable=True}}"/>
</UserControl.Resources>
<UserControl.DataContext>
<Binding>
<Binding.Source>
<local:SampleDataSource />
</Binding.Source>
</Binding>
</UserControl.DataContext>
<StackPanel Margin="20,60">
<TextBlock Style="{StaticResource PageHeaderTextStyle}">Categories</TextBlock>
<ListBox>
<ListBoxItem Content="{Binding Title}" />
</ListBox>
</StackPanel>
The Title property binding is showing in the XAML designer window but nothing is showing during execution of the program.
I am using the default flipview layout in which there is a "Title" in SampleDataSource.cs file

The "d:" prefix is for properties that you would define solely for use in the designer, so you should try replacing "d:Source" with "Source".

did you fix following line
DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
it's defined at the top where all the page declarations are.
Also
Source="{Binding Items}"
is referring to a List named Items in your DataContext.

Related

how to assign resx resource to itemtemplate control

In my WP8 app, i have radio button where customized it through datatemplate and embedded a textblock in it. My question is how to assign a key from my resx resource file to assign text value, so that i can localize these datatemplate control names without any issues.
Below is my sample xaml:
<TextBlock x:Name="lblTileColor" TextWrapping="Wrap" Text="{Binding LocalizedResources.Themes_Tile_label, Source={StaticResource LocalizedStrings}}" />
<RadioButton x:Name="accentColor" GroupName="tileColor" IsChecked="true">
<RadioButton.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Width="25" Height="25" Fill="{StaticResource PhoneAccentBrush}"/>
<TextBlock Width="10"/>
<TextBlock x:Name="lblDefaultAccent" Text="{Binding LocalizedResources.Themes_Tile_Default, Source={StaticResource LocalizedStrings}}" />
</StackPanel>
</DataTemplate>
</RadioButton.ContentTemplate>
</RadioButton>
Here i am trying to assign Themes_Tile_Default resx key to textblock named lblDefaultAccent control.
With above code i am seeing xaml error (i.e. Invalid XAML) in Datatemplate section - if i assign text value directly with hardcoding it is working...
any help to resolve this?

Howto correctly bind RibbonGalleryCategory to a collection

I'm using Microsoft.Windows.Controls.Ribbon.
I want to have a dynamic combobox with picture buttons in my ribbon.
If I do this directly in xaml, I get what I want:
<ribbon:RibbonComboBox
SelectionBoxWidth="62"
VerticalAlignment="Center"
>
<ribbon:RibbonGallery SelectedValue="0"
SelectedValuePath="Content"
MaxColumnCount="1">
<ribbon:RibbonGalleryCategory>
<ribbon:RibbonButton Label="Histo" HorizontalContentAlignment="Stretch"
Command="{Binding NewHistogrammCommand}"
SmallImageSource="/Test;component/Resourcen/Histogramm32.png"
LargeImageSource="/Test;component/Resourcen/Histogramm32.png" />
<ribbon:RibbonButton Label="3D" HorizontalContentAlignment="Stretch"
Command="{Binding NewDreiDCommand}"
SmallImageSource="/Test;component/Resourcen/DreiD32.png"
LargeImageSource="/Test;component/Resourcen/DreiD32.png" />
</ribbon:RibbonGalleryCategory>
</ribbon:RibbonGallery>
</ribbon:RibbonComboBox>
But if I try to do this via binding to a collection this way:
<ribbon:RibbonComboBox
SelectionBoxWidth="62"
VerticalAlignment="Center"
IsEditable="True" >
<ribbon:RibbonGallery
MaxColumnCount="1">
<ribbon:RibbonGalleryCategory ItemsSource="{Binding LayoutContentTypeList, ElementName=mainWindow}">
<ribbon:RibbonGalleryCategory.ItemTemplate>
<DataTemplate>
<ribbon:RibbonButton Label="{Binding Header}" HorizontalContentAlignment="Stretch"
Command="{Binding Command}"
CommandParameter="{Binding CommandParameter}"
SmallImageSource="{Binding ImageSource}"
LargeImageSource="{Binding ImageSource}" />
</DataTemplate>
</ribbon:RibbonGalleryCategory.ItemTemplate>
</ribbon:RibbonGalleryCategory>
</ribbon:RibbonGallery>
</ribbon:RibbonComboBox>
I get
System.Windows.Data Error: 40 : BindingExpression path error: 'IsDropDownOpen' property not found on 'object' ''ContentPresenter' (Name='')'. BindingExpression:Path=IsDropDownOpen; DataItem='ContentPresenter' (Name=''); target element is 'RibbonButton' (Name=''); target property is 'NoTarget' (type 'Object')
The buttons work correctly, but how can I resolve this binding error?
I'm guessing that you've found a solution for your problem, but for other people coming across this post, looking for an answer, you can find a complete solution that you can download and examine at your leisure in the How do I add Galleries to my Ribbon? post at 'The official blog of the Windows Presentation Foundation Team'. The basic idea is as follows.
Whatever object that you set as the RibbonGallery.DataContext should have a collection property to bind to the RibbonGalleryCategory.ItemsSource property. The objects in that collection should have properties containing the values that you want to appear in the gallery items. Declare a HierarchicalDataTemplate for the RibbonGallery.CategoryTemplate to bind your properties. Here is an example from the linked post:
<Ribbon:RibbonGallery DataContext="{x:Static data:WordModel.StylesParagraphGalleryData}"
ItemsSource="{Binding CategoryDataCollection}"
ScrollViewer.VerticalScrollBarVisibility="Hidden">
<Ribbon:RibbonGallery.CategoryTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding GalleryItemDataCollection}">
<Border Background="LightGray">
<TextBlock Text="{Binding}" FontWeight="Bold" />
</Border>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="Images\Paragraph_32x32.png" />
<TextBlock Margin="10,0,0,0" Text="{Binding}" />
</StackPanel>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</Ribbon:RibbonGallery.CategoryTemplate>
</Ribbon:RibbonGallery>

Binding TextBlock foreground to Global.cs variable in xaml

I've been trawling the web trying to find a similar problem but can't find anything that solves my problem.
I have a static class holding global variables, some of these are colour variables which I want to be able to bind in my xaml controls such that I only have to change one variable to change the lot (the reason for global variables).
The class namespace is MyApp.Assets.Resources the class name is Global(.cs).
In my class I have variables named DEFAULTCOLOR, OKCOLOR and ALARMCOLOR.
My xaml:
<UserControl
<!-- ... -->
xmlns:custom="clr-namespace:InspectionDB.Assets.Resources"
>
<UserControl.Resources>
<custom:Global x:Name="global"/> <!-- THIS GIVES AN ERROR SEE BELOW-->
<common:HierarchicalDataTemplate x:Key="Level1" ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Type, Converter={StaticResource localTreeImgConverter}}"/>
<TextBlock {Binding...} />
<StackPanel Orientation="Horizontal" Visibility="{Binding Type, Converter={StaticResource localStatusVisibilityConverter}}">
<TextBlock Margin="5,0, 0,0" Text="[ " Foreground="{Binding DEFAULTCOLOR, Source={StaticResource global}}" />
<TextBlock Text="{Binding Critical}" Foreground="{Binding ALARMCOLOR, Source={StaticResource global}}"/>
<TextBlock Text=" | " Foreground="{Binding DEFAULTCOLOR, Source={StaticResource global}}"/>
<TextBlock Text="{Binding OK}" Foreground="{Binding OKCOLOR, Source={StaticResource global}}"/>
<TextBlock Text=" ]" Foreground="{Binding DEFAULTCOLOR, Source={StaticResource global}}"/>
</StackPanel>
</StackPanel>
</common:HierarchicalDataTemplate>
</UserControl.Resources>
<Grid>
<!-- Where the hierarchical template is used -->
</Grid>
</UserControl>
The part in the Resources throws an error as well as the binding:
Unable to cast object of type 'ApplicationProxy' to type 'Microsoft.Expression.DesignModel.DocumentModel.DocumentNode'.
What is wrong with my code and how can I get it to work?
I found what my problem was, and therefore why I couldn't find any similar situation.
I had public class Global : App - don't ask me why, I'm sure there was a valid reason long ago but it meant it had some kind of funky circular referencing going on.
Problem solved.

How to specify Command binding scope in MVVM?

I'm messing around with MVVM and I've hit a bit of a roadblock with binding commands to buttons. I have a few buttons in a View ( = UserControl) that are generated based on a list of objects I have.
My code looks like this:
(MainWindow)
<ItemsControl ItemsSource="{Binding ViewModels}" Margin="12,57,12,12" />
(UserControl)
<ItemsControl ItemsSource="{Binding AllConnections}" Margin="0,34,0,0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Password}" Height="23" HorizontalAlignment="Left" Margin="114,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Command="{Binding Path=ConnectCommand}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
But ConnectCommand isn't being called and I assume it's because XAML is looking for it in the AllConnections binding, rather than the ViewModels binding where it should. How do I specify this?
You should use Relative source to specify the ancestor. Something like this:
Command = "{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=ViewModel.CommandToBind}"
You can add your command to Resources and just use {StaticResource yourCommand}. This could significally simplify xaml.
Useful links: WPF Commands, How to declare Application level commands?Commands as XAML ResourcesMVVM Commanding inside of a datatemplate

How to get control parent DataContext for element to element databinding?

Suppose I have a user control which datacontext is bound to a VM. This VM has a property for a list MyList.
Inside this user control I have a ComboBox, I want to set following kind of xaml
<Grid x:Name="LayoutRoot" Background="White">
<StackPanel HorizontalAlignment="Stretch">
<sdk:DataGrid ItemsSource="{Binding YourList}" IsReadOnly="True" AutoGenerateColumns="False" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<!-- ...... -->
<sdk:DataGridTemplateColumn Header="User" >
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding ElementName=LayoutRoot, Path=DataContext.MyList}" DisplayMemberPath="Value" SelectedValuePath="Key" SelectedValue="{Binding UserID}" ></ComboBox>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</StackPanel>
</Grid>
but it is not working.
How to resolve this problem?
This worked form me. This was the ItemSource for a ComboBox that was within a DataGrid:
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=sdk:DataGrid},
Path=DataContext.Teams}">
Are you trying to get to the main VM from within the UserControl? take a look at this solution. http://weblogs.asp.net/dwahlin/archive/2009/08/20/creating-a-silverlight-datacontext-proxy-to-simplify-data-binding-in-nested-controls.aspx