How to add UserControl inside Window and assign x:Name - c#

First of all sorry for my english and sorry again, I'm beginner and maybe I try to do something that is not possible.
My goal is create a UserControl to reuse the code in different windows/pages/usercontrols using different parameters. I developed a class to print boxes to track objects over a video.
Well my problem is while compiling I recieve the follow error:
Name 'mediaBoxes' does not exist in the current context
If I change:
<local:MediaBoxes x:Name="mediaBoxes"></local:MediaBoxes>
instead of
<local:MediaBoxes Name="mediaBoxes"></local:MediaBoxes>
I recieved the follow error:
The type name MediaBoxes does not exist in the type MyModule
If I remove the attribute Name and I do not call to InitVideo, all code compile, and can execute perfect.. but my player is always loading because I need initialize the instance.
Simplifying my code, I have:
InfoPage.xaml
<Window x:Class="MyModule.InfoPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MyModule"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance local:InfoPage}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="PlayerBoxes" />
<DockPanel Grid.Row="1" Name="containerVideo">
<local:MediaBoxes Name="mediaBoxes"></local:MediaBoxes>
</DockPanel>
</Grid>
</Window>
InfoPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using Fulcrum.Model;
using Genetec.Sdk;
using Genetec.Sdk.Workspace;
using MyModule.Helpers;
namespace MyModule
{
public partial class InfoPage : Window
{
#region Constructors
public InfoPage(SearchPage mainTaskUc, ODetection det)
{
InitializeComponent();
_detection = det;
mainTask = mainTaskUc;
DataContext = this;
}
#endregion
#region Public Methods
public void Initialize(Workspace workspace)
{
if (workspace == null)
throw new ArgumentNullException("workspace");
Workspace = workspace;
_sdkEngine = Workspace.Sdk;
Show();
mediaBoxes.InitVideo(mainTask.SearchSelectedClips[0], _detection, _sdkEngine);
}
#endregion
}
And the mediaBoxes is a user control with:
MediaBoxes.xaml
<UserControl x:Class="MyModule.MediaBoxes"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:MyModule"
xmlns:media="clr-namespace:Genetec.Sdk.Media;assembly=Genetec.Sdk.Media"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../Styles/Colors.xaml"></ResourceDictionary>
<ResourceDictionary Source="../Styles/Fonts.xaml"></ResourceDictionary>
<ResourceDictionary Source="../Styles/Texts.xaml"></ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<DockPanel Name="containerVideo" Grid.Row="0">
<Canvas Name="canvas" Background="{StaticResource GreyDark4Brush}"
Width="{Binding ActualWidth, ElementName=mediaPlayer}"
Height="{Binding ActualHeight, ElementName=mediaPlayer}"
Visibility="Collapsed">
<media:MediaPlayer Name="mediaPlayer"
Width="{Binding ActualWidth, ElementName=containerVideo}"
Height="{Binding ActualHeight, ElementName=containerVideo}" />
<Canvas Name="CanvasBoxes" />
<Canvas Name="CanvasControls">
<TextBlock Name="plyrBoxesCameraTitle" Canvas.Top="24" Canvas.Left="24" Foreground="LightGray" />
<TextBlock Name="plyrBoxesDateTime" Canvas.Top="56" Canvas.Left="24" Foreground="LightGray" />
<Button Name="plyrBoxesBookmarkBtn" Canvas.Top="24" Canvas.Right="24">
<Button.Content>
<TextBlock FontFamily="{StaticResource MaterialIcons}" Text=""
Foreground="LightGray" />
</Button.Content>
</Button>
<Grid Canvas.Bottom="0" Canvas.Left="0"
Width="{Binding ActualWidth, ElementName=CanvasControls}">
<Grid.Resources>
<Style TargetType="Border">
<Setter Property="Padding" Value="24" />
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border Grid.Column="0" HorizontalAlignment="Left">
<DockPanel>
<Button Name="plyrBoxesVolumeOnBtn">
<Button.Content>
<TextBlock FontFamily="{StaticResource MaterialIcons}" Text=""
Foreground="LightGray" />
</Button.Content>
</Button>
<Button Name="plyrBoxesVolumeOffBtn" Visibility="Collapsed">
<Button.Content>
<TextBlock FontFamily="{StaticResource MaterialIcons}" Text=""
Foreground="LightGray" />
</Button.Content>
</Button>
</DockPanel>
</Border>
<Border Grid.Column="1" HorizontalAlignment="Center">
<DockPanel>
<Button Name="plyrBoxesPlayBtn" Visibility="Collapsed">
<Button.Content>
<TextBlock FontFamily="{StaticResource MaterialIcons}" Text=""
Foreground="LightGray" />
</Button.Content>
</Button>
<Button Name="plyrBoxesPauseBtn">
<Button.Content>
<TextBlock FontFamily="{StaticResource MaterialIcons}" Text=""
Foreground="LightGray" />
</Button.Content>
</Button>
<Button Name="plyrBoxesRestartSequenceBtn">
<Button.Content>
<TextBlock FontFamily="{StaticResource MaterialIcons}" Text=""
Foreground="LightGray" />
</Button.Content>
</Button>
</DockPanel>
</Border>
<Border Grid.Column="2" HorizontalAlignment="Right">
<TextBlock Name="plyrBoxesCurrentTime" Foreground="LightGray" />
</Border>
</Grid>
</Canvas>
</Canvas>
<StackPanel Name="loadingVideoPlayer" Visibility="Visible"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<TextBlock Style="{StaticResource LoadingSpinningText}" FontSize="52" Width="52" Height="52" />
<TextBlock FontSize="18" Margin="0 16">Loading Video</TextBlock>
</StackPanel>
</DockPanel>
</Grid>
</Grid>
and MediaBoxes.xaml.cs
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Threading;
using Genetec.Sdk;
namespace MyModule
{
public partial class MediaBoxes : UserControl
{
public MediaBoxes()
{
InitializeComponent();
}
private IEngine _sdkEngine;
private SearchSelectedClipModel _selectedCamera;
private ODetection _detection;
public void InitVideo(SearchSelectedClipModel selectedCamera, ODetection detection, IEngine sdkEngine)
{
_sdkEngine = sdkEngine;
_selectedCamera = selectedCamera;
_detection = detection;
...
}
A lot of thanks!!!
UPDATE:
Ok, I found the real problem.. My code didn't have any problem... But I had defined a class in root path with the same name of my namespace. -.-'
Thanks all for your help!!

Some ui things can have a Name, some can't. You don't need to name your control though.
Your usercontrol is a frameworkelement and it will therefore have the standard events.
That included initialized and loaded.
You could hook either of those with an eventhandler via XAML.
<local:MediaBoxes Initialized="MediaBoxes_Initialized"/>
If you type in initialized= then you can accept the stub event handler it gives you.

You need to give name as per following
<local:MediaBoxes x:Name="mediaBoxes"></local:MediaBoxes>
because your usercontrol 'MediaBoxes' and 'Window' are in same namespace (MyModule) so if you will write following
<local:MediaBoxes Name="mediaBoxes"></local:MediaBoxes>
you will get following error
Because 'Microsoft.VisualStudio.DesignTools.Xaml.LanguageService.Semantics.Metadata.ReflectionTypeNode' is implemented in the same assembly, you must set the x:Name attribute rather than the Microsoft.VisualStudio.DesignTools.Xaml.LanguageService.Semantics.Metadata.ReflectionPropertyNode attribute.
So give name to you usercontrol in following way
<local:MediaBoxes x:Name="mediaBoxes"></local:MediaBoxes>
and in code behind access it using following
mediaBoxes.InitVideo(mainTask.SearchSelectedClips[0], _detection, _sdkEngine);

Related

How to open child window in mainwindow in MMVM WPF

I have 3 screens
Mainwindow.xaml
<Window x:Class="PatientAdminTool.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Window.DataContext>
<vm:MainViewModel></vm:MainViewModel>
</Window.DataContext>
<WindowChrome.WindowChrome>
<WindowChrome
CaptionHeight="0"/>
</WindowChrome.WindowChrome>
<ContentControl >
<v:PatientWindow DataContext="{Binding PatientVM}"/>
</ContentControl>
</Window>
PatientWindow.xaml
<UserControl x:Class="PatientAdminTool.View.PatientWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:PatientAdminTool.ViewModel"
xmlns:v="clr-namespace:PatientAdminTool.View"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" >
<UserControl.DataContext>
<vm:PatientSelectorVM/>
</UserControl.DataContext>
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<DockPanel Grid.Row="0" LastChildFill="True" Height="40" Background="#646161" >
<StackPanel DockPanel.Dock="Left" Orientation="Horizontal">
<TextBlock Margin=" 10,5,0,0" HorizontalAlignment="Center" Text="Patients" FontSize="16" TextAlignment="Center" VerticalAlignment="Center" Foreground="#FFFFFF"/>
</StackPanel>
<StackPanel Margin="10,10,0,0" DockPanel.Dock="Right" Background="Transparent" Orientation="Vertical" HorizontalAlignment="Right" VerticalAlignment="Top" Width="50" >
<StackPanel Background="Transparent" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Focusable="False" ToolTip="Close" VerticalAlignment="Center" Background="#646161" BorderThickness="0" BorderBrush="Transparent" Padding="-4" Command="{Binding CloseCommand,Mode=TwoWay}">
<Button.Content>
<Grid Width="45" Height="23">
<TextBlock Foreground="White" Text="r" FontFamily="Marlett" FontSize="20" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
</Button.Content>
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter Content="{TemplateBinding Content}"/>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</StackPanel>
</DockPanel>
<ContentControl Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<v:PatientSelector DataContext="{Binding PatientSelectorVM}" />
</ContentControl>
</Grid>
</UserControl>
Result should be as per the below image
I need to show the child window by using corresponding view model.
I am not getting how to do this?
You can't access UI elements (including windows) in ViewModels directly.
Just make abstraction of the functionality "Open Window". The easiest way is to use interface:
interface IDialogService
{
public void OpenWindow(BaseViewModel windowViewModel);
}
//in viewmodel:
void OpenPatientWindow_CommandExecuted()
{
var patientWindowVM = new PatientWindowViewModel())
patientWindowVM.Parameter = "This way you can pass parameters";
_dialogService.OpenWindow(patientWindowVM);
}
go here for more inspiration: Handling Dialogs in WPF with MVVM
Bind the Content property of ContentPresenter to the PatientVM property of the MainViewModel in MainWindow.xaml and use define a DataTemplate per child view/view model type:
<ContentControl Content="{Binding PatientVM}">
<ContentControl.Resources>
<DataTemplate DataType="local:PatientSelectorVM">
<v:PatientWindow />
</DataTemplate>
<DataTemplate DataType="local:ThirdScreenType">
<v:ThirdScreenView />
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
You then set the PatientVM property of the MainViewModel to a PatientSelectorVM object to display the PatientWindow view and to a ThirdScreenType object (or whatever your class is actually called) to display the other view in the ContentControl.
Make sure that the MainViewModel implements the INotifyPropertyChanged interface and raise the PropertyChanged event in the setter of the PatientVM property:
public BaseViewModel PatientVM
{
get { return _patientVM; }
set
{
patientVM = value;
OnPropertyChanged();
}
}
For you to be able to set the property to both a PatientSelectorVM object and the other type of objects, these classes should derive from the same base class or implement the same interface.

ViewModelLocator in PRISM (Unity) does not work in modules

I am following the Brian Lagunas's tutorial and have a question.
We have a small application setup with PRISM 6 and Unity bootstrapper. I want to use the ViewModelLocator to bind a ViewModel to my view. This works in the "base module" (the one with Shell and bootstrapper), but it seems that there are some issues when using it in other modules.
Below you can find the classes for View (XAML and code behind) and for ViewModel. The binding works if I set the DataContext in the code behind manually. while debugging the code I found out that the ViewModel class is never instantiated so I guess the VML cannot find the VM because of wrong configuration. But as far as I can see the naming conventions are fulfilled.
Can you find the issue or have an idea what configuration I did forget?
View(code behind):
namespace CommunicationModule.Views
{
public partial class CommunicationView : UserControl, IView
{
public CommunicationView()
{
InitializeComponent();
}
}
}
View XAML:
<UserControl
x:Class="CommunicationModule.Views.CommunicationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:View="clr-namespace:CommunicationModule.Views"
xmlns:prism="http://prismlibrary.com/"
prism:ViewModelLocator.AutoWireViewModel="True">
<Grid DataContext="{Binding ModelList, UpdateSourceTrigger=PropertyChanged}" Width="320" Height="300">
<Grid.Resources>
<DataTemplate x:Key="DataTemplate">
<Border Name="border" BorderBrush="DarkSlateBlue" BorderThickness="2"
CornerRadius="2" Padding="5" Margin="5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition MaxWidth="30"/>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Image Grid.Row="0" Grid.Column="0" Source="{Binding Path=IconUri}" HorizontalAlignment="Left" />
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Title}" FontSize="12" FontWeight="Bold" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=ContentShort}"/>
</Grid>
</Border>
</DataTemplate>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!-- UI -->
<DockPanel Grid.ColumnSpan="2" Margin="0,0,10,0">
<!-- Title -->
<Label DockPanel.Dock="Top" FontSize="18" Margin="5" Content="Wichtige Meldungen"/>
<!-- Data template is specified by the ItemTemplate attribute -->
<ScrollViewer>
<ListBox Name="listBox"
SelectionMode="Single"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource DataTemplate}"
HorizontalContentAlignment="Stretch"
IsSynchronizedWithCurrentItem="True"
Margin="5,0,5,5" Width="280"/>
</ScrollViewer>
</DockPanel>
</Grid>
</UserControl>
ViewModel:
namespace CommunicationModule.ViewModels
{
public class CommunicationViewViewModel : BindableBase
{
private List<CommunicationModel> _modelList = (new CommunicationModelBO()).getCommunicationItems(); //= new List<CommunicationModel>();
private readonly IRegionManager _regionManager;
public List<CommunicationModel> ModelList
{
get { return _modelList; }
set { SetProperty<List<CommunicationModel>>(ref _modelList, value); }
}
public CommunicationViewViewModel(IRegionManager regionManager)
{
_regionManager = regionManager;
}
}
}
You can not let your usercontrol's name end with View,if you do so, the AutoWireViewModel won't work. So please change your usercontrols name to Communication and viewmodels name to CommunicationViewModel.

UserControl element inaccessible due to its protection level

I am writing a Windows Phone 8.1 App (WinRT).
my user control:
XAML:
<UserControl x:Name="LoadingProgressBarPage"
x:Class="Project1.Custom.General.UserControls.LoadingOverlayFullScreen"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Merakyahoga.com.Custom.General.UserControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
Foreground="{x:Null}"
d:DesignHeight="800" d:DesignWidth="480" >
<Grid x:Name="LayoutRoot">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height="1*"/>
</Grid.RowDefinitions>
<StackPanel
Name="LayoutRoot_StackPanelMain"
Grid.Row="1"
Orientation="Vertical">
<ProgressBar Name="ProgressBar"
IsIndeterminate="True"
Foreground="{StaticResource DefaultTheme_DarkBlueColor}"
Height="50"
Width="480"
VerticalAlignment="Center"/>
<StackPanel Name="LayoutRoot_SubStackPanel"
Orientation="Horizontal"
HorizontalAlignment="Center"
VerticalAlignment="Center">
<Image Name="ImageHolder_Main"
Width="54">
</Image>
<TextBlock Name="ProgressBarText"
Text="Please Wait.."
Margin="10,0,0,0"
FontWeight="Normal"
HorizontalAlignment="Center"
FontSize="18"
Foreground="{StaticResource DefaultTheme_DarkBlueColor}" VerticalAlignment="Center"/>
</StackPanel>
</StackPanel>
</Grid>
cs:
namespace Project1.Custom.General.UserControls
{
public partial class LoadingOverlayFullScreen : UserControl
{
public LoadingOverlayFullScreen()
{
InitializeComponent()
//WINRT:
this.LayoutRoot.Height = Window.Current.Bounds.Height;
this.LayoutRoot.Width = Window.Current.Bounds.Width;
}
}
}
In my mainpage.cs:
LoadingOverlayFullScreen LoadingOverlayFullScreenObject = new LoadingOverlayFullScreen();
//test:
LoadingOverlayFullScreenObject.ProgressBarText = "Hello"; //ERROR Here >> **element inaccessible due to its protection level**
By naming the TextBlock element, you cause the XAML compiler to generate a class member having that name, i.e. ProgressBarText. But the accessibility for that member is not public, but rather internal.
If you really want for the field to be public, you can add the x:FieldModifier attribute:
<TextBlock Name="ProgressBarText"
x:FieldModifier="public"
Text="Please Wait.."
Margin="10,0,0,0"
FontWeight="Normal"
HorizontalAlignment="Center"
FontSize="18"
Foreground="{StaticResource DefaultTheme_DarkBlueColor}"
VerticalAlignment="Center"/>
That said, it would be better to expose the Text property of the TextBlock object as a string property in your code-behind. Or even better, use data-binding to control the Text value of the TextBlock (but your code example is not complete enough for me to say how exactly you would do that).

Windows Phone C# - Settings Page - Initialization

I am creating a Windows Phone Application but i have a small problem.
I am using the AppSettings Class from MSDN samples to save my settings and this is working fine.
BUT, inside my settings UI i have a Radio Inputs. When each Radio Input is being checked i want to make Visibility.Collaped or Visibility.Visible a TextBox. It does not allow me to do this beucause i guess when the AppSettings is being initialized and a Radio Input is checked every toolbox is null.
The Settings page is being initialized after the AppSettings so how i am gonna do this?
If i run the below code it gives me nullreferenceexception in the line:
EnterRadiusBox.Visibility = Visibility.Collapsed;
I hope you understand me.
Here is the Code i Have So Far in The Settings Page:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace MyAPP
{
public partial class Page1 : PhoneApplicationPage
{
public Page1()
{
InitializeComponent();
}
private void OffersFromRadius_Checked(object sender, RoutedEventArgs e)
{
EnterRadiusBox.Visibility = Visibility.Visible;
RadiusExplain.Visibility = Visibility.Visible;
}
private void OffersFromCity_Checked(object sender, RoutedEventArgs e)
{
EnterRadiusBox.Visibility = Visibility.Collapsed;
RadiusExplain.Visibility = Visibility.Collapsed;
}
}
}
And Here is my Settings.xaml
<phone:PhoneApplicationPage
x:Class="MyAPP.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d"
xmlns:local="clr-namespace:SettingsHandle"
ApplicationBar = "{StaticResource GlobalAppBar}"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
<local:AppSettings x:Key="appSettings"></local:AppSettings>
</phone:PhoneApplicationPage.Resources>
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!--TitlePanel contains the name of the application and page title-->
<StackPanel Grid.Row="0" Margin="12,17,0,28">
<TextBlock Text="MyAPP" Style="{StaticResource PhoneTextNormalStyle}"/>
<TextBlock Text="Settings" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="14,0,10,0">
<TextBlock HorizontalAlignment="Left" Margin="151,27,0,0" TextWrapping="Wrap" Text="Show Offers From" VerticalAlignment="Top" Height="30" Width="167"/>
<RadioButton x:Name="OffersFromCity" Content="My City" HorizontalAlignment="Left" Margin="67,57,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.818,0.428" GroupName="GroupOne" IsChecked="{Binding Source={StaticResource appSettings}, Path=OffersFromCitySetting, Mode=TwoWay}" Checked="OffersFromCity_Checked" />
<RadioButton x:Name="OffersFromRadius" Content="A Radius" HorizontalAlignment="Left" Margin="246,57,0,0" VerticalAlignment="Top" GroupName="GroupOne" IsChecked="{Binding Source={StaticResource appSettings}, Path=OffersFromRadiusSetting, Mode=TwoWay}" Checked="OffersFromRadius_Checked" />
<TextBox x:Name="EnterRadiusBox" HorizontalAlignment="Left" Height="72" Margin="92,124,0,0" TextWrapping="Wrap" Text="60" VerticalAlignment="Top" Width="296" InputScope="Number" />
<TextBlock x:Name="RadiusExplain" HorizontalAlignment="Left" Margin="56,196,0,0" TextWrapping="Wrap" Text="I want to hide this value or make it visible." VerticalAlignment="Top" Height="84" Width="364" />
</Grid>
</Grid>
</phone:PhoneApplicationPage>
This should better be solved with XAML bindings, instead of C# code.
Add the Cimbalino lib to your project or add a BooleanToVisibilityConverter on your own.
If you use Cimablino, add the following namespace to the <Application
...></Application> root node inside your App.xaml:
xmlns:conv="clr-namespace:Cimbalino.Phone.Toolkit.Converters;assembly=Cimbalino.Phone.Toolkit"
and add this converter below <Application.Resources> in the App.xaml:
<conv:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
Then you can bind the visibility of your TextBox/TextBlock to the matching Radiobox in your own code:
<RadioButton x:Name="OffersFromCity" GroupName="GroupOne" ... />
<RadioButton x:Name="OffersFromRadius" GroupName="GroupOne" ... />
<TextBox x:Name="EnterRadiusBox" Visibility="{Binding IsChecked, ElementName=OffersFromRadius, Converter={StaticResource BooleanToVisibilityConverter}}" ... />
<TextBlock x:Name="RadiusExplain" Visibility="{Binding IsChecked, ElementName=OffersFromRadius, Converter={StaticResource BooleanToVisibilityConverter}}" ... />
In the Binding of the Visibility you say that the property IsChecked from the element with the name OffersFromRadius should be used. Because the Visibility property does not know what a bool is, we need the converter. This converter translates the bool to the matching Visibility.

XamlReader throwing up undeclared prefix error

I defined a resource as follows:
<DataTemplate xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Name="grdArticle" Height="190" Width="190" toolkit:TiltEffect.IsTiltEnabled="True">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Image Source="samplePhoto.jpg" Grid.Row="0" Stretch="UniformToFill"/>
<Grid Grid.Row="0" Height="55" VerticalAlignment="Bottom">
<Rectangle Grid.Row="0" Fill="{StaticResource PhoneAccentBrush}"/>
<TextBlock Grid.Row="0" Text="{Binding Title}" Canvas.ZIndex="2" VerticalAlignment="Bottom" TextWrapping="Wrap" Margin="5,5,5,5" Height="50" FontStyle="Normal">
<TextBlock.Foreground>
<SolidColorBrush Color="#BFFFFFFF"/>
</TextBlock.Foreground>
</TextBlock>
</Grid>
</Grid>
</DataTemplate>
When I'm trying to parse it using:
DataTemplate dtTmplt = XamlReader.Load(PhoneApp5.Resource1.Datatemplate_lst) as DataTemplate;
I'm getting XamlParseException,saying "undeclared prefix" at line 2,position 104.
I have tried many xmlns tags from the internet,but nothing seems to work.Any help?
It's the toolkit prefix that is undeclared. You need to add
xmlns:toolkit="..."
to either the <DataTemplate> or the outer <Grid> element. Replace ... with whatever you're binding xmlns:toolkit to elsewhere in your application.
Incidentally, is there a reason you're parsing the contents of a resource as XAML this way? Normally, I'd put a DataTemplate in a Resources resource-dictionary somewhere, or in Application.Resources. That way, the compiler would check that it is valid XAML.

Categories