I would like to use a TemplateSelector to select a View. Not for ListViewItems, like every example out there shows, but with a "normal" View. So i tried TemplatedView, ContentPresenter and ContentView. But non of them is able to take a TemplateSelector.
Is there something i have missed? Or how can i work around that?
EDIT:
I have a TemplateSelector, just like described here. Now i want to add this Selector to some kind of ViewElement. Thats what i tried:
<TemplatedView ControlTemplate="{StaticResource ViewItemTemplateSelector}"/>
or
<ContentPresenter Content="{StaticResource ViewItemTemplateSelector}"/>
But nothing works, it always says "Invalid resource type"
EDIT2:
<ResourceDictionary MergedWith="dataTemplates:DataTemplates">
<helper:ViewItemTemplateSelector x:Key="ViewItemTemplateSelector"
TextDataTemplate="{StaticResource TextDataTemplate}"/>
Have you tried following this guide of Xamarin?
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/templates/data-templates/selector/
Hope this works out for you.
EDIT:
Perhaps an alternative solution:
There are control templates but i don't think there is a selector for it. You could maybe use triggers for it? https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/triggers/#Data_Triggers not sure if this is way you looking
Related
I am a newbie to Visual Studio and its languages...
I have search but couldn't find an answer. Maybe I didn't search with the right syntax.
Here is what I want to do.
<TextBlock x:Name="test">Hello World</TextBlock>
I believe there is a way to target the TEXTBLOCK control with the X:NAME attribute or any other attribute using C# like for example in HTML and JS I can do something like this
<div id="test></div>
Then I can target the Element through its ID in JS like this
div = document.getElementById('test');
I believe I can do similar in C#
Please any idea?
XAML
<TextBlock x:Name="TestTextBlock">Hello World</TextBlock>
CodeBehind
TestTextBlock.Text = "blerg";
Though on saying this, XAML Likes to be data bound, and the common way of doing this is using MVVM, i would start looking into this personally
While creating a simple custom expander, I encountered the problem where items inside IT wouldn't bind. I found the fix on this link:
http://codeoverload.wordpress.com/2012/03/04/wpf-expander-headertemplates-dont-forget-the-binding/
Which happens to treat that exact same issue, however what I understand from it is "found this by luck, not really sure why it worked ;D"
My question being now: why does adding Header={Binding} fixes the issue. Indeed from the fact binding wouldn't work, it seems it's due to the DataContext, but I don't see how this should fix it.
Thanks for explaining; hopefully this isn't a duplicate >.<
From the docs
Gets or sets the data used for the header of each control.
That object being a binding against the DataContext, or plain text, or whatever.
I'd bet the implementation looks to see if the value is text, and if so, throws it into the default header template which could be something as simple as
<TextBlock Text="{Binding}" />
If you declare a template for the header, the DataContext will be whatever you assign to the Header property. The DataContext of the Expander doesn't flow automatically to the header template, apparently.
I have found very little information about this matter. Know that I am a newbie to C# and WPF:
I have a stack panel defined in a XAML file as such :
<StackPanel Orientation="Vertical" >
<TextBlock Text="Locale: " VerticalAlignment="Center"/>
<ComboBox x:Name="comboLocale" Width="60" VerticalAlignment="Center" SelectionChanged="comboLocale_SelectionChanged"/>
</StackPanel>
I want to disable the highlighting that happens when I MouseOver the stack panel, which creates a blue color inside the StackPanel for some reason. I don't have any special style set up yet. Some threads talked about setting OverridesDefaultStyle to TRUE, but this didn't seem to change anything. Also, StackPanel do not have a ControlTemplate available, so most of the solutions I found couldn't be applied since they refer to a Button or TextBlock.
Any input on the matter would be greatly appreciated!
-Regards
StackPanels in general have no visual representation and are just layout containers which control placement of other elements. Given that you haven't set anything like Background on your StackPanel, it isn't what's causing the highlight you're seeing unless some other part of your XAML or code is modifying it. The behavior you describe sounds like the default behavior of a Button but without seeing more of your code it's hard to tell where the behavior is coming from.
Since you mentioned in a comment you've found out you're actually looking at the expected behavior of a Menu as your culprit. You'll just need to edit the MenuItem Control Template, more specifically the IsHighlighted that's causing your highlight. You'd likely find something like this helpful.
Or there's lots more various information found with a quick search for customizing a WPF Menu / MenuItem, hope this helps.
I'm working on a project which requires its listBox.ItemTemplate to be neat, so I tried to design it out with a new page, then using expression blend to drag it into a ListBox. However, it turns into "destroyed" ui. What should I know in order to design ListBox.ItemTemplate content in xaml with expression blend?
Thanks
Expression blend allows to design item templates right in ListBox.
Created template should looks like:
<ListBox>
<ListBox.ItemTemplate>
<DataTemplate>
<!-- Layout here -->
<DataTemplate>
<ListBox.ItemTemplate>
<ListBox>
Check this very detailed article on CodeProject on how to define and style the ItemTemplate for a ListBox: ListBox styling - Additional templates in Expression Blend. It is for Silverlight in general but the principle will be the same for WP7 (as this is a subset of Silverlight). Hope this helps!
To create a custom ListBox see the following Microsoft tutorial. Also, to import/inherit a control from VS2010 you could follow the methods outlined in part 1 and part 2 of a tutorial provided by TheWindowsClub.
There were tutorials by Microsoft, but I have check my favourites list and they have been taken down for some strange reason. Hope this helps...
I had the same problem, I've been searching for the solution for 2 hours and this is the solution:
Editing ListBoxItem styles and templates
if it seems too complicated, I'll write a blog post about it in one week, I'll share it.
I cannot for the life of me figure out how to change a ToggleButton image when clicked. I have looked up countless examples and they are all outdated and no longer work. Or if they do I cannot get them to work. Does anyone have an up to date example I could look at or any suggestions?
I tried doing it in the code behind first. The example I found used a BitmapImage but this is not possible anymore as the BeginInit method cant be used due to security reasons.
Next I tried numerous style triggers but I get way to many compile errors even when they are directly copied and modified to fit the correct parameters. So I am stuck. I cant figure out how to use an EventTrigger to do it nor do any older examples seem to work. Anyone have any ideas?
Why not something like:
<ToggleButton x:Name="b">
<Image Src="myImage.png" Visibility="{Binding ElementName=b,Path=IsChecked,Converter="{StaticResource BooleanToVisibilityConverter}}"/>
<Image Src="myOtherImage.png" Visibility="{Binding ElementName=b,Path=IsChecked,Converter="{StaticResource BooleanToVisibilityConverter,ConverterParameter=Invert}}"/>
</ToggleButton>
Where you have a boolean to visibility converter that can accept a parameter to invert the bool.
Edit:
You'll need to define a converter so that it can convert the bool? from the IsChecked property to a Visibility enum. That's what all the binding code does. There is a basic implementation here that will convert to Visibility.Visible when true and Visibility.Collapsed when false. You need to add a check for the parameter so that it inverts the visibility when Invert is passed (to toggle between two images).
The other way to do this is to define images in the style and use the visual states for Checked and Unchecked to flip flop the images. You can apply a style to multiple buttons but it's hard to vary the images per-button (what my solution does).
This is how you set up a Resource
XAML
<!-- Place this in your window -->
xmlns:converters="clr-namespace:NameSpace"
<!-- Place this above your root UI -->
<Window.Resources>
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</Window.Resources>
Then use the converter here BooleanToVisibilityConverter
you can use ImageToggleButton and ImageToggleButtonSideBySide (if you want a Mac-style toggle [or 2-state radio could call it] button) from ImageButtons project of ClipFlair project codebase