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.
Related
I'm trying to use a SplitView whose Buttons come from data binding. Here is the code:
<SplitView Name="AnnouncementSplitView"
DisplayMode="CompactInline"
OpenPaneLength="200"
CompactPaneLength="56"
HorizontalAlignment="Left" Grid.Row="1">
<ListView ItemsSource="{x:Bind Parts}"
SelectionMode="Single"
Name="CongregationList"
SelectionChanged="LoadAnnouncements">
<ListView.ItemTemplate>
<DataTemplate x:DataType="data:Part">
<StackPanel Orientation="Horizontal">
<TextBlock Name="PartImage"
Text=""
FontFamily="Segoe MDL2 Assets"/>
<TextBlock Name="PartName"
Text="{x:Bind PartName}"
Margin="20,0,0,0"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</SplitView>
I get an error at the following line:
x:DataType="data:Part"
that the class Part does not exist in the namespace defind by data although it does..
The mistake was that that I had created the class after I wrote the code in XAML. So when it was written the compiler tried to find a nonexisting class. All I had to do is rewrite the XAML code, clean and rebuild the solution...
I am developing a windows 8 metro app in which I have listbox which contains a set of textblocks and a Image.
<ListBox x:Name="lstbxbStudents" Background="Transparent" ItemContainerStyleSelector="{StaticResource ItemStyleSelector}" ScrollViewer.VerticalScrollBarVisibility="Auto" ItemTemplate="{StaticResource LandscapeItemTemplate}" Height="476" SelectionChanged="lstbxbProducts_SelectionChanged_1" Style="{StaticResource ListBoxStyle1}" HorizontalAlignment="Left" Width="901">
</ListBox>
For that image ImgCmt I have set the source of the Image static inside the datatemplate of the listbox.
<Page.Resources>
<CollectionViewSource x:Name="cvs2" IsSourceGrouped="true" />
<!-- TODO: Delete this line if the key AppName is declared in App.xaml -->
<x:String x:Key="AppName">Students Screen</x:String>
<DataTemplate x:Key="LandscapeItemTemplate" >
<StackPanel Orientation="Horizontal">
<StackPanel Width="30"></StackPanel>
<StackPanel Width="120" Orientation="Horizontal">
<TextBlock Text="{Binding stunum}" VerticalAlignment="Center" HorizontalAlignment="Left" />
</StackPanel>
<StackPanel Width="350">
<TextBlock Text="{Binding studsc}" HorizontalAlignment="Left" />
</StackPanel>
<StackPanel Width="10"></StackPanel>
<StackPanel Width="100">
<TextBlock Text="{Binding stuum}" x:Name="txtblkstuum" HorizontalAlignment="Left" />
</StackPanel>
<StackPanel Width="150">
<TextBlock Text="{Binding stugrp}" VerticalAlignment="Center" TextAlignment="Right" HorizontalAlignment="Center" />
</StackPanel>
<StackPanel Width="100">
<TextBlock Text="{Binding stusection, Mode=TwoWay}" TextAlignment="Center" x:Name="txtbxbstusection" Tag="{Binding stunum}" VerticalAlignment="Center" HorizontalAlignment="Right" />
</StackPanel>
<StackPanel Width="50"></StackPanel>
<StackPanel>
<Image Source="Assets/comments.png" Name="ImgCmt" PointerPressed="Image_PointerPressed_1" VerticalAlignment="Center" Width="20" Height="20"></Image>
</StackPanel>
</StackPanel>
</DataTemplate>
</Page.Resources>
my objective is that I want to change the source of the image to different image source(change the image) in codebehind depnding upon some condition for that I need to access the control present inside a datatemplate of listbox in metro app ?
How can I do this :
How to access a control present inside a datatemplate of listbox in metro app?
What are the different ways in which I can do this?
How can I change the source of the image to different image source(change the image) in codebehind depending upon some condition?
This is a common question. We've all asked it at least once. The problem is that these controls don't have a unique name, because they are in a repeater. As a result, you cannot use the logical tree in XAML. The logical tree is what lets you call things by name. Instead, you need to use the visual tree in XAML. The visual tree is what lets you access everything on the screen including the dynamically rendered elements that adorn controls and populate repeaters. Because the visual tree is so big and because a repeater repeats, you still have to constrain the scope of the visual tree so you can reliably locate the control you want to find. I hope this makes sense.
Solution here: http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html
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?
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>
Here is the XAML:
<ListBox ItemsSource="{Binding Documents}" BorderBrush="{x:Null}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Title}" FontSize="12" FontWeight="Bold" />
<TextBlock Text="{Binding ID}" FontSize="10" FontStyle="Italic" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ContextMenu>
<ContextMenu ItemsSource="{Binding CategoryList}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Command="{Binding AddDocumentToCategoryContextMenuCommand}" Header="{Binding Category.Name}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
Ok so the ListBox's ItemSource is bound to the Documents collection in the VM and properly renders the Titles and IDs
The Context Menu's ItemSource is bound to the CategoryList collection in the VM and properly renders the list of categories.
The problem I have is with the Command Binding:
Command="{Binding AddDocumentToCategoryContextMenuCommand}"
Since the ItemSource for the ContextMenu is already set, it tries to get the AddDocumentToCategoryContextMenuCommand from CategoryList. Obviously the command is not there, it is a member of the VM.
I do not want any references to the VMs or Models in the XAML. Everything is constructed using Unity and VM-View is associated in App.xaml:
<Application.Resources>
<DataTemplate DataType="{x:Type vms:FeedViewModel}">
<views:FeedView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vms:DocumentsViewModel}">
<views:DocumentsView/>
</DataTemplate>
<DataTemplate DataType="{x:Type vms:ManagementViewModel}">
<views:ManagementView/>
</DataTemplate>
<DataTemplate DataType="{x:Type dev:DevelopmentViewModel}">
<dev:DevelopmentView />
</DataTemplate>
</Application.Resources>
How can I databind to a member of the VM from within the ContextItem.
Thanks.
UPDATED edit #1 starts Here
Here is the updated xaml (but still not working but some insight gained):
<ListBox ItemsSource="{Binding Documents}" x:Name="Results" BorderBrush="{x:Null}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Title}" FontSize="12" FontWeight="Bold" />
<TextBlock Text="{Binding ID}" FontSize="10" FontStyle="Italic" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ContextMenu>
<ContextMenu ItemsSource="{Binding CategoryList}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Command="{Binding ElementName=Results, Path=DataContext.AddDocumentToCategoryContextMenuCommand}" Header="{Binding Category.Name}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
I have this example working for a simple example that does not use a ContextMenu. It appears that the ContextMenu (even though attached to the ListBox) is not part of the user control visual tree. The binding always comes back null / not found. I think the ContextMenu, because it is a floating "window" is constructed in its own tree and therefore cannot find the ListBox call "Results" in order to access the ListBox's DataContext.
Any thoughts on this? Any recommendations on how deal with?
Edit #2 Starts Here
In case you are are wondering, figured out the answer to the binding question:
This binding works:
Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.DataContext.AddDocumentToCategoryContextMenuCommand}
Hope this helps others with the same question.
One last update for completeness.
In order for the command to know which context menu item was clicked on, I had to change the xaml slightly (silly oversight):
<ListBox.ContextMenu>
<ContextMenu x:Name="Context" ItemsSource="{Binding CategoryList}">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Command="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.DataContext.AddDocumentToCategoryContextMenuCommand}"
CommandParameter="{Binding Category.ID}"
Header="{Binding Category.Name}" />
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
</ListBox.ContextMenu>
Again, hope this helps others.
Use the ElementName property of the binding to specify that. You'd end up with something like this:
Command="{Binding ElementName=ViewModelObject
Path=AddDocumentToCategoryContextMenuCommand}"