Xaml - The name is already defined in this scope - c#

I have defined a "Toggle" textblock inside three buttons. I am getting an error that the name "Toggle" Is already defined in the innermost scope of the second and third buttons.
Shouldn't they not be defined in the same scope? It seems they are...
If this is not the way to do it, how can one simply define similar items (Similar to how there can be an id property in css) so one can access the inside of each button via one same identifier in the code behind?
<Button Name="OutputButton" Margin="5" Width="150" Click="OutputToggle_Click" TabIndex="0" HorizontalAlignment="Center">
<TextBlock Name="OutputText" HorizontalAlignment="Center">RF
<TextBlock Name="Toggle" HorizontalAlignment="Left"></TextBlock>
</TextBlock>
</Button>
<Button Name="ModulationButton" Margin="5" Width="Auto" HorizontalAlignment="Left" Click="ModulationToggle_Click" IsEnabled="False" TabIndex="0">
<TextBlock Name="ModulationToggleText" Margin="3">Load Waveform to enable MOD
<TextBlock Name="Toggle"></TextBlock>
</TextBlock>
</Button>
<Button Name="LOSharingButton" Margin="5" Width="150" HorizontalAlignment="Left" Click="AutomaticLOSharingButton_Click">
<TextBlock Name="LOSharingText">LO Sharing
<TextBlock Name="Toggle"></TextBlock>
</TextBlock>
</Button>

The answer to this is to use the tag property of a textblock. This is very similar to the id property in css, but takes a little more effort to find.

Related

How to create own GUI component in WPF?

i am trying to do some people administration application and i would like to use own component. I have a listBox with actions and i would like to show people who are in list in selected action. There is no problem for me to show just their names in another list, but i would like to have a pane where would be a card (my component) for every person asociated with that action. That card should look like
<WrapPanel HorizontalAlignment="Left" Height="296" Margin="668,59,0,0" VerticalAlignment="Top" Width="230">
<Image x:Name="image" Height="124" Width="226"/>
<TextBlock x:Name="textBlock_Copy5" TextWrapping="Wrap" Text="Name: " FontSize="18"/>
<TextBox x:Name="textBox_Copy2" Height="25" TextWrapping="Wrap" Width="151"/>
<TextBlock x:Name="textBlock_Copy6" TextWrapping="Wrap" Text="Surname: " FontSize="18"/>
<TextBox x:Name="textBox_Copy3" Height="25" TextWrapping="Wrap" Width="145"/>
<TextBlock x:Name="textBlock_Copy7" TextWrapping="Wrap" Text="Birthday: " FontSize="18"/>
<DatePicker Width="136" DisplayDate="2016-06-27" DisplayDateStart="1950-01-01" FirstDayOfWeek="Monday" SelectedDate="{Binding Selected.DatumOdjezdu}"/>
</WrapPanel>
So there are two problems for me.
How to create own component.
How to create a pane(it can be scrollable), where would be as many those cards as needed.
Thanks for any hint.
WPF offers more than one way to achieve your goal.
It depends on your Application, what fits your needs the most.
You can reach this by
DataTemplates
UserControl
CustomControl
Here some more Info about the Differences of UserControl and CustomControl
Depending on your posted Code, i'd suggest a DataTemplate in your case.

Settings screen for app is throwing an exception

I was trying to use the settings flyout in phone 8.1 runtime to produce a settings screen for an app. However the compiler is throwing an error and I dont no why. This would have worked fine in phone 8.0 but not 8.1 runtime it seems.
<SettingsFlyout
x:Class="popcornpk.Settings"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:popcornpk"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
IconSource="Assets/SmallLogo.png"
Title="CustomSetting" >
<!-- This StackPanel acts as a root panel for vertical layout of the content sections -->
<StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
<!-- Toggle switch -->
<StackPanel >
<TextBlock Text="Toggle Switch" Style="{StaticResource TitleTextBlockStyle}"/>
<TextBlock Margin="0,0,0,25" Text="Use toggle switches to let users set Boolean values." Style="{StaticResource BodyTextBlockStyle}"/>
<ToggleSwitch Margin="-6,0,0,0" Header = "Download updates automatically" HorizontalAlignment="Left" HorizontalContentAlignment="Left"/>
<ToggleSwitch Margin="-6,0,0,0" Header = "Install updates automatically" HorizontalAlignment="Stretch"/>
</StackPanel>
<!-- Button -->
<StackPanel >
<TextBlock Text="Push button" Style="{StaticResource TitleTextBlockStyle}"/>
<TextBlock Text="Button label" Style="{StaticResource BodyTextBlockStyle}"/>
<Button Margin="-3,0,0,0" Content="Clear"/>
<TextBlock Margin="0,0,0,25" Text="With a push button, users initiate an immediate action." Style="{StaticResource BodyTextBlockStyle}"/>
</StackPanel>
<!-- ComboBox -->
<StackPanel >
<TextBlock Text="ComboBox" Style="{StaticResource TitleTextBlockStyle}"/>
<TextBlock Margin="0,0,0,25" Text="Use the ComboBox to allow users to select one item from a set of text-only items." Style="{StaticResource BodyTextBlockStyle}"/>
<ComboBox Header="State:" Margin="0,7,0,0" SelectedIndex="0" HorizontalAlignment="Left">
<ComboBoxItem Content="Washington"/>
<ComboBoxItem Content="Oregon"/>
<ComboBoxItem Content="California"/>
</ComboBox>
</StackPanel>
<!-- HyperlinkButton -->
<StackPanel >
<TextBlock Text="Hyperlink" Style="{StaticResource TitleTextBlockStyle}"/>
<TextBlock Margin="0,0,0,25" Text="Use a hyperlink when the associated action will take the user out of this flyout." Style="{StaticResource BodyTextBlockStyle}"/>
<HyperlinkButton Padding="-5,0,0,0" Content="View privacy statement" Tag="http://privacy.microsoft.com" HorizontalAlignment="Left"/>
</StackPanel>
<!-- TextBox -->
<StackPanel >
<TextBlock Text="TextBox" Style="{StaticResource TitleTextBlockStyle}"/>
<TextBlock Margin="0,0,0,25" Text="Use a TextBox to allow users to enter text." Style="{StaticResource BodyTextBlockStyle}"/>
<StackPanel Margin="0,7,0,0" Orientation="Horizontal">
<TextBox HorizontalAlignment="Left" Width="150"/>
<Button Margin="20,0,0,0" Content="Add"/>
</StackPanel>
</StackPanel>
<!-- RadioButton -->
<StackPanel>
<TextBlock Text="Radio button group" Style="{StaticResource TitleTextBlockStyle}"/>
<TextBlock Margin="0,0,0,25" Text="Lets users choose one item from a small set of mutually exclusive, related options." Style="{StaticResource BodyTextBlockStyle}"/>
<TextBlock Text="Video quality" Style="{StaticResource BodyTextBlockStyle}"/>
<RadioButton Margin="0,7,0,0" Content="High"/>
<RadioButton Margin="0,17,0,0" Content="Medium"/>
<RadioButton Margin="0,17,0,0" Content="Low"/>
</StackPanel>
</StackPanel>
Severity Code Description Project File Line
Error CS0263 Partial declarations of 'Settings' must not specify different base classes popcornpk C:\Users\david\Documents\Visual Studio 2015\81StoreLiveAppsPhoneOnlyLIVE\popcornpk\popcornpkhub\popcornpk\popcornpk\obj\Debug\Settings.g.i.cs 15
My base class is just the same as other pages
Note 2
public partial class Settings : Page
I changed above to settings and it compiled but the flyount did not work or popup so it didnt any idea as to why it didnt.
Note 2
To Avoid confusion I dont want a base page of page I wanted to be able to use the settingsflyout that was part of 8.0 apparently microsoft have dicthced this though for 8.1 UAP
The exception tells you everything you need to know.
Look at your xaml. Here's the top two lines of your file:
<SettingsFlyout
x:Class="popcornpk.Settings"
This says that you're declaring a new class called popcornpk.Settings (btw, namespaces should be PascalCased) that extends the base class SettingsFlyout.
When you add a new xaml file to your application, it actually creates three files--the .xaml file, a .xaml.cs file that you edit, and a mysterious ephemeral .g.i.cs file that holds designer-generated code (e.g., the implementation of InitializeComponent()).
The .xaml.cs and .g.i.cs files are partial class files, and are combined by the compiler. Note what the exception message says--
Partial declarations of 'Settings' must not specify different base classes
What are these different base classes?
Well, from the xaml, we can see the first one--SettingsFlyout.
From your snippet,
public partial class Settings : Page
We see the other base class--Page.
Not sure how you screwed that up, but the simple fix is to remove the base class declaration from your .xaml.cs file:
public partial class Settings
{
// snip
Apart from what Will has said about partial classes, which will surely solve the error, you probably won't be able to use SettingsFlyout on Phone - following the remarks:
Caution SettingsFlyout is supported only for use with the SettingsPane in Windows 8. While the SettingsFlyout type is visible in Windows Phone projects, SettingsPane is not present on Windows Phone, so use of SettingsFlyout is not supported.

HubSection without DataTemplate (or can you have a form in a HubSection)?

Porting an iOS app and I may be abusing the hub control; but I have a very simple app where I'd like a form to be in a hub section. However it seems like it must be wrapped in a DataTemplate and once it is the form's TextBox controls do not show up in the code behind C# file?
<HubSection x:Uid="HubSection1" Header="SETTINGS"
DataContext="{Binding Groups[2]}" HeaderTemplate="{ThemeResource HubSectionHeaderTemplate}">
<DataTemplate>
<StackPanel VerticalAlignment="Top" Margin="0,0,20,0">
<TextBlock Text="api base uri" FontSize="21" TextWrapping="NoWrap"/>
<TextBox Name="apiBaseUriTextBox" Margin="0,0,0,10" TextWrapping="Wrap" PlaceholderText="api base uri" VerticalAlignment="Stretch"/>
<TextBlock Text="api key" FontSize="21" TextWrapping="NoWrap" Margin="0,0,0,10"/>
<TextBox Name="apiKeyTextBox" Margin="0,0,0,10" TextWrapping="Wrap" PlaceholderText="api key" VerticalAlignment="Stretch"/>
<TextBlock Text="api secret" FontSize="21" TextWrapping="NoWrap"/>
<TextBox Name="apiSecretTextBox" Margin="0,0,0,10" TextWrapping="Wrap" PlaceholderText="api secret" VerticalAlignment="Stretch"/>
<Button Click="SettingsSaveClickHandler"
Content="Save" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,20,0,0"/>
</StackPanel>
</DataTemplate>
</HubSection>
If you want to put the form directly in the HubSection's DataTemplate then the best way to access the values is by data binding the TextBoxes to properties in your DataContext. The other option would be to crawl the visual tree by calling the VisualTreeHelper from your button's SettingsSaveClickHandler
Another option would be to create your form as a UserControl and then host the completed form in the HubSection's DataTemplate. Again, you can bind it to the DataContext to connect to the underlying data.

How do I Change HyperlinkButtons' image according to the page that is currently displayed

I have a bunch (currently) HyperlinkButtons on my main web page. I want to have 2 versions for the image of each button: selected and unselected. This way when the users enters a page the corresponding button will change to the "selected" image.
Here is an image of what I want to accomplish:
This seems to me like something trivial, but so far I have been running into a stone wall.
I would prefer to do everything from the XAML (but I will be grateful for any solution).
Here is a little of my XAML:
<ScrollViewer x:Name="NavScrollViewer" Margin="-5,12,5,-12" ScrollViewer.VerticalScrollBarVisibility="Visible" IsEnabled="True"
Style="{StaticResource ContentViewerStyle}">
<StackPanel x:Name="ToolboxPanel" Orientation="Vertical" d:LayoutOverrides="Width" Height="Auto">
<HyperlinkButton x:Name="DashboardButton"
Content="Assets/icon_dashboard.png"
Style="{StaticResource ToolStyle}"
TargetName="ContentFrame"
NavigateUri="/Dashboard"
Height="50"
/>
<TextBlock Text="Dashboard" HorizontalAlignment="Center" Height="20" Style="{StaticResource ComponentNameStyle}"/>
<HyperlinkButton x:Name="ConfigurationButton"
Content="Assets/icon_dashboard.png"
Style="{StaticResource ToolStyle}"
TargetName="ContentFrame"
NavigateUri="/CRSConfiguration"
Height="50"
/>
<TextBlock Text="Configuration" HorizontalAlignment="Center" Height="20" Style="{StaticResource ComponentNameStyle}"/>
<HyperlinkButton x:Name="ScanEnginestionButton"
Content="Assets/icon_dashboard.png"
Style="{StaticResource ToolStyle}"
TargetName="ContentFrame"
NavigateUri="/ScanEngines"
Height="50"
/>...
You need to have a toggle button and set its IsChecked property to toggle among its visual states. You may go through this post Using Toggle Button

How to use an accesskey on a WPF Button with a custom ContentTemplate?

Scenario:
Currently I have this XAML code:
<Button Content="_Cancel" IsCancel="True" Command="{Binding Path=CancelCommand}" Margin="5">
<Button.ContentTemplate>
<DataTemplate>
<TextBlock Margin="10,0,10,0" />
</DataTemplate>
</Button.ContentTemplate>
</Button>
Obviously the accesskey (the 'c' key: _Cancel) doesn't work in combination with the TextBlock. I actually think the TextBlock should be a ContentPresenter (below), but this crashes my Visual Studio 2010 instance every time.
<ContentPresenter Margin="10,0,10,0" RecognizesAccessKey="True" />
Question:
What's the best solution to use accesskeys on a WPF Button with a ContentTemplate?
Thanks in advance!
Instead of TextBlock use AccessText thus:
<Button Content="_Cancel" IsCancel="True" Command="{Binding Path=CancelCommand}" Margin="5">
<Button.ContentTemplate>
<DataTemplate>
<AccessText Margin="10,0,10,0" Text="{Binding}"/>
</DataTemplate>
</Button.ContentTemplate>
</Button>
PS. ContentPresenter should be used inside a ControlTemplate to display content according to a DataTemplate. If you use it within a DataTemplate it causes infinite recursion as the DataTemplate is invoked over and over again.

Categories