I have the following AppBar.
<Page.BottomAppBar>
<AppBar x:Name="bottomAppBar" Padding="10,0,10,0">
<Grid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
<Button x:Name="switchMeasurementMode" AutomationProperties.Name = "Breath rate" Style="{StaticResource AppBarButtonStyle}" Click="switchMeasurementMode_Click" />
</StackPanel>
</Grid>
</AppBar>
</Page.BottomAppBar>
It looks like this
I tend to change its text during run-time with the following C# code
private void switchMeasurementMode_Click(object sender, RoutedEventArgs e)
{
this.switchMeasurementMode.Name = "111";
}
But the button text is not changed. Is there anything I had missed out?
If you use the default styles for AppBar in Windows 8 C# projects, then you have to change the attached property AutomationProperties.Name either in XAML using:
AutomationProperties.Name = "new name"
or in code using:
Button.SetValue(AutomationProperties.NameProperty, "new value");
or
AutomationProperties.SetName(Button, "new value");
The button is a content control. You set the Content property to change the content.
this.switchMeasurementMode.Content= "111";
The Name property is how you set the programmatic "handle" for the Button. You use the name in the code editor to modify the control. In your case, you are changing the name, which means you loose the ability to say this.switchMeasurementMode...
FYI, content can be more than just text. Most XAML elements can be added as content.
Related
I'm trying to programatically create a button flyout, within my XAML I have:
<Page.Resources>
<Button x:Key="LaunchFlyout" Content="LAUNCH">
<Button.Flyout>
<Flyout Placement="Top">
<Grid Width="200" Height="200">
<StackPanel>
<Rectangle Fill="Red" Width="100" Height="100" />
<Rectangle Fill="Green" Width="100" Height="100" />
</StackPanel>
</Grid>
</Flyout>
</Button.Flyout>
</Button>
</Page.Resources>
Nested within grids I have:
<Grid x:Name="launchBtn_grid" Grid.Column="1">
</Grid>
And then in my code within the Page_Loaded method I have:
bool hasContainer = localSettings.Containers.ContainsKey("appStatus");
if (!hasContainer) {
Button button = (Button)this.Resources["LaunchFlyout"];
launchBtn_grid.Children.Add(button);
}
else {
Button button = new Button();
button.Content = "LAUNCH";
button.Click += launch_btn_Click;
launchBtn_grid.Children.Add(button);
}
When I debug this, it reaches the IF statement and reaches this line launchBtn_grid.Children.Add(button); and then I get this error Element is already the child of another element.
Does anyone understand why? I have already looked and they dont already exist so I don't understand why it is giving me this error. Does anyone know what I am doing wrong?
I'm not sure in what context/use case your are doing that, but it feels weird to me to have an actual control as a Resource (not a DataTemplate, Style, etc).
If you only want to have 1 button of the 2 different template, why not switch Visibility on the 2 instead of loading controls from your code behind ?
Going forward with the idea, just add both buttons in the Grid within your XAML and switch their Visibility according to the setting you read.
There is a BooleanToVisibilityConverter within the framework to help you with this.
I have a Label here in XAML. If I set the content in the XAML side, it will display. But when I try to set the content in code behind, it doesn't display as it shows:
My code:
if (Application.Current.Resources["Values"] != null)
{
string score = Application.Current.Resources["Values"].ToString();
labelscore.Content = score;
}
I have checked the vale of the resource, the value is there but I can't display it.
My XAML side:
<Label Height="30" Width="100" Name="labelscore" FontWeight="Bold" FontSize="15" />
Try temporarily changing your line of code to
labelscore.Content = "TEST STRING";
and comment out the following line
if (Application.Current.Resources["Values"] != null)
If the Label displays the text, then your Resources are the problem.
Alternatively, try binding the value from your code behind to the Label. This is generally the preferred method when using WPF.
<Label Height="30" Width="100" Binding="{Binding Values}" ... />
I need to create a button with two lines of text:
The first one is Command Title like "Save"
The second one is a Description of the Command like "The application state will be saved"
So I have written the next xaml:
<Button Margin="0,128,0,0" Padding="10,5" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<StackPanel Margin="0" UseLayoutRounding="False">
<TextBlock FontSize="{StaticResource PhoneFontSizeMediumLarge}" FontFamily="{StaticResource PhoneFontFamilySemiBold}">Save</TextBlock>
<TextBlock Style="{StaticResource PhoneTextSubtleStyle}" Margin="0">The application state will be saved</TextBlock>
</StackPanel>
</Button>
This code working well except a one issue. The Description line becomes invisible when the button is pushed.
I'm sure the root cause is the low contrast color of the description line. But I don't know how to fix it.
Update: I have tried to use the PhoneTextSubtleStyle style but still have the same issue.
You could retemplate the Button (using the Control.Template property) to look different so that when pushed it no longer interferes with the content.
Could you try something like this
System.Windows.Visibility.Visible;
System.Windows.Visibility.Hidden;
or
System.Windows.Visibility.Collapsed
here is a link that will show an example of how to use this inside of a StackPanel
How to: Change the Visibility Property
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?
In my custom control I want to programmaticaly enable or disable tooltip depending on options. Here is how my icon defined in template:
<Image x:Name="PART_IconImage" Stretch="None" VerticalAlignment="Top" HorizontalAlignment="Center" Source="{TemplateBinding Icon}"
ToolTipService.ToolTip="{TemplateBinding Caption}" />
I'm using this code to access ToolTip and to enable/disable it:
// Enable tooltip when caption not shown
if (this.IconImage != null)
{
var toolTip = ToolTipService.GetToolTip(this.IconImage) as ToolTip;
if (toolTip != null)
toolTip.IsEnabled = this.CaptionVisibility.HasValue
? (this.CaptionVisibility.Value == Visibility.Collapsed)
: (this.ParentToolbar.CaptionsVisibility == Visibility.Collapsed);
}
GetToolTip returns null. Any idea why?
P.S. I was following this advice here: How to programmatically access ToolTipService of a Silverlight FrameworkElement?
But it doesn't work for me.
Are you sure that ToolTipService.GetToolTip is returning null, as opposed to returning something other than a ToolTip?
I did a quick experiment with code similar to yours and found that ToolTipService.GetToolTip returned a string. I was of course binding ToolTipService.ToolTip to a string dependency property. I suspect you're also getting a string back from GetToolTip, but the as ToolTip you have added after the call to this method nulls out this string.
One way to programmatically disable the tooltip is to bind it to a property on the view-model which contains the tooltip text if the tooltip should be shown or null if the tooltip should not be shown.
Alternatively, you can use a ToolTip, instead of a string, as the tooltip for your control. That way you should be able to access the ToolTip object and enable/disable it in your code above:
<Image x:Name="PART_IconImage" Stretch="None" VerticalAlignment="Top" HorizontalAlignment="Center" Source="{TemplateBinding Icon}">
<ToolTipService.ToolTip>
<ToolTip>
<TextBlock Text="{TemplateBinding Caption}" />
</ToolTip>
</ToolTipService.ToolTip>
</Image>
Why don't you simply bind property below with a bool property?
ToolTipService.IsEnabled
Then whenever you want to disable/enable simply change the binded property
< Image ToolTipService.IsEnabled="{Binding Path=SomeProperty}">
Also take a look at How do you disable tooltips in code at runtime