Binding the value of an UIElement within GridViewDataColumn.Header not working - silverlight-4.0

I am trying to show a circle in a telrik gridview and I wanted to bind the colour dynamically to this circle.Since I am working with MVVM pattern I have to bound my view model to the datacontext of my page.But the binding does not seems to be working for me.When I investigated the issue was because the column headers does not have any datacontext, so I tried bound the value using 'ElementName' and tried to use its datacontext but even that is also not working.
Could anyone please help me resolving this issue.
This is my xaml code
<UserControl x:Class="TelrikStyling.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerikGrid="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:myColour="clr-namespace:TelrikStyling"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" x:Name="myMainPage">
<Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">
<telerikGrid:RadGridView x:Name="radGridView"
AutoGenerateColumns="False">
<telerikGrid:RadGridView.Columns>
<telerikGrid:GridViewDataColumn>
<telerikGrid:GridViewDataColumn.Header>
<StackPanel Orientation="Horizontal">
<Ellipse x:Name="headerEllipse" Width="20" Height="20" Fill="{Binding ElementName=myMainPage, Path=DataContext.Colour}"></Ellipse>
</StackPanel>
</telerikGrid:GridViewDataColumn.Header>
</telerikGrid:GridViewDataColumn>
</telerikGrid:RadGridView.Columns>
</telerikGrid:RadGridView>
</Grid>
</UserControl>
This is my view model
public MainPage()
{
InitializeComponent();
this.DataContext = new MainPageViewModel { Colour = new SolidColorBrush(Colors.Red) };
}

Try using RelativeSource instead of ElementName in your Binding
I've had similar issues in the past where I couldn't resolve a binding using ElementName but could with RelativeSource. I think it may have to do with the timing of resolving the binding.
<Ellipse Fill="{Binding RelativeSource={RelativeSource
AncestorType={x:Type myColour:MainPage}},
Path=DataContext.Colour}" />

I have found a solution for my problem that is by adding new ellipse to the layout as a child of the layout grid and then bind the Colour property of the viewmodel to the Fill property of new ellipse.Later I have bound Fill property of the ellipse in the RadGrid to the newly added ellipse.
This is the new xaml
<UserControl x:Class="TelrikStyling.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:telerikGrid="http://schemas.telerik.com/2008/xaml/presentation"
xmlns:myColour="clr-namespace:TelrikStyling"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" x:Name="myMainPage">
<Grid x:Name="LayoutRoot" Background="White" Loaded="LayoutRoot_Loaded">
<telerikGrid:RadGridView x:Name="radGridView"
AutoGenerateColumns="False">
<telerikGrid:RadGridView.Columns>
<telerikGrid:GridViewDataColumn>
<telerikGrid:GridViewDataColumn.Header>
<StackPanel Orientation="Horizontal">
<Ellipse x:Name="headerEllipse" Width="20" Height="20" Fill="{Binding ElementName=invisibleEllipse, Path=Fill}"></Ellipse>
</StackPanel>
</telerikGrid:GridViewDataColumn.Header>
</telerikGrid:GridViewDataColumn>
</telerikGrid:RadGridView.Columns>
</telerikGrid:RadGridView>
<Ellipse x:Name="invisibleEllipse" Width="20" Height="20" Fill="{Binding Colour}"></Ellipse>
</Grid>
</UserControl>
Can anyone tell me why the binding was not working when I gives myMainPage as the element name and it started working when I given a new elisple?
Untill someone answer to this question, I am marking this as an answer.

Related

Set controls DataContext while binding other properties to parent context

I have the following class structure setup.
public class Control1
{
public Control1()
{
Control2Model = new Control2();
}
public Control2 Control2Model {get; set;}
public bool IsControlTwoVisible => true;
}
In my xaml I have a UserControl called CustomUserControl that I want to pass Control2Model to. I also want to bind IsControlTwoVisible to my CustomUserControls Visibility property.
<UserControl x:Class="Control1">
<StackPanel>
<!--Other controls above this-->
<CustomUserControl
DataContext="{Binding Control2Model }"
Visibility="{Binding IsControlTwoVisible, Converter={StaticResource VisibilityConverter}" />
</StackPanel>
</UserControl>
But I get a binding error because CustomUserControl cannot find IsControlTwoVisible on Control2Model.
Is there any way I set CustomUserControls context while still having access to Control1s properties?
Note I want to avoid moving IsControlTwoVisible to Control2.
I would suggest to not set DataContext in the Control1 but instead to the top Grid inside CustomUserControl. By doing that it will solve your problem.
Please refer to below code for reference:
Control1.xaml
<UserControl x:Class="Control1">
<StackPanel>
<!--Other controls above this-->
<CustomUserControl
Visibility="{Binding IsControlTwoVisible, Converter={StaticResource VisibilityConverter}" />
</StackPanel>
</UserControl>
CustomUserControl.xaml
<UserControl
x:Class="UWPBlankApp.CustomUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UWPBlankApp"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid DataContext="{Binding Control2Model}">
<!--Other controls-->
</Grid>
</UserControl>

Binding ViewModel property to the primary View or sub view if primary view including sub view?

I have a primary View. It includes FirstSubView and SecondSubView.
<navigation:Page x:Class="Test.Views.PreimaryView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:localViews="clr-namespace:Test.Views"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
d:DesignWidth="1057" d:DesignHeight="707"
Title="TestView Page">
<UserControl.Resources>
<silverlightLib:BooleanVisibilityConverter x:Key="BooleanVisibilityConverter"></silverlightLib:BooleanVisibilityConverter>
</UserControl.Resources>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Background="Transparent">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<localViews:FirstSubView DataContext="{Binding VmFirst, Mode=TwoWay}"></localViews:FirstSubView>
<localViews:SecondSubView DataContext="{Binding VmSecond, Mode=TwoWay}" Visibility="{Binding IsAdjustVisible, Converter={StaticResource BooleanVisibilityConverter}}"></localViews:SecondSubView>
</StackPanel>
</StackPanel>
</ScrollViewer>
You see in the SecondSubView I have a binding property IsAdjustVisible.
My question is where an I set it? I set up it in both ViewModels to adjust the visibility of the second View. But it doesn't work, even when it is false, the View is still shown.
Finally I found the issue. I only need one place to put the properties. Which is the sub-View rather than the Main View.

Making the image as a background (Send to back)

I would like to have an image in this grid that serves as a background. Any attempt till now at inserting an image control resulted in the image being on top of the buttons while I would like it to be on the back.
<Page
x:Class="ZimmerFrei_v0._1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ZimmerFrei_v0._1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid >
<Button x:Name="btnNearMe"
Content="Near Me"
HorizontalAlignment="Left"
Margin="99,181,0,0" VerticalAlignment="Top"
Height="76"
Width="216"
Click="btnNearMe_Click"/>
<Button x:Name="btnBestOffers"
Content="Button"
HorizontalAlignment="Left"
Margin="99,312,0,0"
VerticalAlignment="Top"
Height="76" Width="216"
Click="Button_Click"/>
<Image />
</Grid>
The simplest thing that you can do here is to give your Grid an Image Brush. Simply head to the Brush section in your properties windows and select the Tile Brush there. To the ImageSource property specify the image you want.
You can also check out this link for more help.
Hope this solves your problem.

Binding height and width of border to half of its children at runtime in windows 8.1 app

I have a user control which have elements like this
<UserControl
x:Class="App2.MyImageControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid x:Name="MyGrid" x:FieldModifier="public" ManipulationMode="None" Tapped="MyGrid_Tapped" HorizontalAlignment="Left" VerticalAlignment="Top" >
<Grid.RenderTransform>
<CompositeTransform></CompositeTransform>
</Grid.RenderTransform>
<Border x:Name="MyBorder" x:FieldModifier="public" BorderThickness="1" BorderBrush="Transparent" Width="auto" Height="auto">
<Grid>
<Image x:Name="MyImageO" x:FieldModifier="public" Source="" Width="auto" Height="auto" Stretch="Fill"></Image>
</Grid>
</Border>
</Grid>
I want to bind MyBorder width and height to half of MyImageO width and height at runtime.How do I achieve that .
You could try using the MultiplyConverter to convert a bound value to half of it, but ActualWidth and ActualHeight aren't bindable - they don't raise change notifications. Your best bet is to simply handle SizeChanged events and then set the dimensions of the border based on ActualWidth and ActualHeight of the image.

How to define implicit data templates in Metro XAML?

I am trying to create a DataTemplate for mapping a simple data type with a corresponding view as follows:
<DataTemplate DataType="{x:Type src:Person}">
<TextBox Text="{Binding Name}"/>
</DataTemplate>
I get a compiler error indicating that the DataType property is not recognized or accessible. Am I missing something here? Is there new syntax for doing this or is the feature missing? Are there alternative solutions for implicit templates?
For reference, here is the full code with the DataTemplate qualified using a x:Key attribute (which works):
<UserControl x:Class="Metro_App.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:src="clr-namespace:Metro_App"
mc:Ignorable="d"
d:DesignHeight="768" d:DesignWidth="1366">
<UserControl.Resources>
<DataTemplate x:Key="PersonTemplate">
<TextBlock Text="{Binding Name}" Foreground="White" FontSize="72"/>
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="#FF0C0C0C">
<ContentControl Content="{Binding MyPerson}" ContentTemplate="{StaticResource PersonTemplate}"/>
</Grid>
</UserControl>
With WinRT, the syntax for mapping your CLR namespaces to XAML are different. You should change you mapping from:
xmlns:src="clr-namespace:Metro_App"
to
xmlns:src="using:Metro_App"
For further information on moving from Silverlight to WinRT, see the series of blog posts by Morten Nielsen, or the article I wrote about creating a cross platform Silverlight / WinRT application.
However ... if you look at the API documentation for DataTemplate you will find that there is not DataType property. Within WinRT there is implicit styling, but not implicit data templating.
Silverlight doesn't have DataTemplate.DataType, and I suspect that Windows XAML framework inherited that limitation. You might have to use DataTemplateSelector instead.
Interestingly enough, it does have DataTemplateKey class, but instantiating it from XAML does not work.
Have you define namespace?
xmlns:src="clr-namespace:WpfApplicationNamespace"
<Window x:Class="WpfApplicationNamespace.MainWindow"
xmlns:src="clr-namespace:WpfApplicationNamespace"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type src:Persone}"/>
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<Button Content="fffff" Click="Button_Click" />
</StackPanel>
</Grid>
</Window>