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
Related
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
I'm writing an app in XAML, and I'm using binding for getting values to the UI layer. I'd like to see what my control will look like while making changes to the XAML, but because the data values are bound, many areas show up as blank (which, in turn, messes up the relative layout).
Is there any way to give XAML values to use for rendering the control review without replacing the Binding directives?
You can also use design time data to see how your xaml works. You just need to add new class that will be treated as design time view model. Its more elegant way to test xaml at design time.
Maybe you can set TargetNullValue or FallbackValue property in your binding, example:
<TextBlock Text="{Binding NotExsitOrNullPropertyName, TargetNullValue=SomeDefaultValue, FallbackValue=SomeDefaultValue}" ></TextBlock>
Hope it hepls.
Is there any possibility to use XPath in order to identify each XAML element in running application?
For example if I have following XAML structure.
<Page>
<StackPanel>
<Button />
<Button />
</StackPanel>
</Page>
I would like to know XPath of the second button for instance and when I have it, I would like to retrieve the button thanks to XPath afterwards.
I know that I can work with XPath without any problems if I have XmlDocument but I would like to achieve the same stuff with elements from running application if it is possible.
There is actually no such XAML structure to apply an XPath on at runtime. The XAML parser will parse your XAML markup and create a tree of elements that make up the user interface that you see on the screen.
So if you need to find the second button in a StackPanel once it has been constructed at runtime you should either look directly in the StackPanel's Children collection:
Button secondButton = sp.Children[1] as Button;
...or use the VisualTreeHelper class to find it in the visual tree: Find control in the visual tree
I am updating some xaml I have written to instead use the code behind due to an issue cropping up.
Pane refers to a Telerik RadPane object.
What I need to do semantically is :
pane.Content = PaneView.xaml;
PaneView being a xaml file containing multiple elements and info. This will not work.
I had this working as follows in the previous xaml file, so it is possible to do; though I don't know how. Can anyone help ?
<UserControl x:vws="MyProject.ViewFolder.Views">
...
<telerik:RadPane Header="PaneView" CanUserClose="False" CanFloat="False"
telerik:RadDocking.SerializationTag="PaneView">
<vws:PaneView />
</telerik:RadPane>
What wrapper do I need to put my xaml file in to force this to work?
Thanks very much
If your RadPane control (the XAML) is defined as x:Class="SomeNameSpaceHere.PaneView" you can set the content pretty simply via:
pane.Content = new PaneView();
You can use a simple UserControl as a container:
<UserControl x:Class="SomeNameSpaceHere.PaneView">
...
</UserControl>
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.