how to put array of user control in the columns of Grid - silverlight

Hi I have array of Employee data which includes photo,description, title name,salary. I have to show each employee data in separate panel control structure that means if I have 10 employees there will be 10 panels. and I have to show these panels in a grid which has two columns. Also width of the panel in each column will vary according to main page size.

Why not represent the employee data with a separate userControl then use a Wrap panel to display the employees. This approach would handle a variable number of employees.
To better illustrate my idea here's some code:
The MainPage has a grid with a WrapPanel that fills the available space.
MainPage.xaml
<UserControl
x:Class="SilverlightApplication2.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:ct="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
mc:Ignorable="d">
<Grid x:Name="LayoutRoot" Background="WhiteSmoke">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition/>
<RowDefinition Height="200"/>
</Grid.RowDefinitions>
<ct:WrapPanel x:Name="panel1" Grid.Row="1" Grid.Column="1"
Background="Aqua" VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"/>
</Grid>
</UserControl>
MainPage.xaml.cs
public partial class MainPage : UserControl
{
public MainPage ()
{
InitializeComponent();
this.DataContext = this;
this.Loaded += new RoutedEventHandler( MainPage_Loaded );
}
void MainPage_Loaded ( object sender, RoutedEventArgs e )
{
for ( int x = 0; x <= 10; x++ )
{
panel1.Children.Add( new ChildControl() );
}
}
}
I am adding ChildWindow controls as defined below to show how the WrapPanel adapts the presentation of its children to the space available.
ChildWindow.xaml
<UserControl
x:Class="SilverlightApplication2.ChildControl"
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"
Width="100"
Height="100">
<Grid x:Name="LayoutRoot" Background="PowderBlue">
<TextBlock Text="ChildControl"/>
</Grid>
</UserControl>
If I've missed your point please give some more clarification.

Related

How to access a user control in a given cell of XAML grid?

I have a XAML grid for my WPF project which is located inside a page called MonthPage.xaml with content as below
<Page x:Class="Calander.Views.pages.MonthPage"
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:Calander"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="Page1">
<Grid x:Name="month">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<local:day Grid.Row="0" Grid.Column="0"/>
<local:day Grid.Row="0" Grid.Column="1" />
<local:day Grid.Row="0" Grid.Column="2" />
</Grid>
</Page>
I gave the the grid an x:name "month" and in each cell I added a user control called day, here is the xaml code foy day.xaml:
<UserControl x:Class="Calander.day"
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:Calander"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Grid>
<Button Background="White" BorderThickness="0 1 0 0" x:Name="cellbutton"/>
</Grid>
</UserControl>
You can see that there is a button in my day user control. Now my question is how can I use the x:name of my grid (I called it "month") in MonthPage.xaml.cs to change the content of the button for each day user control in the grid using c# code. My sudo code is something like this:
month.Children.Gridindex(column, row).day.button.content = "somestring"
You first need to find the day element in the Grid's Children collection based on its Grid.Row and Grid.Column values, and then you could use the FindName method to get a reference to the Button.
Try this:
int row = 0;
int column = 1;
day day = month.Children.OfType<day>().FirstOrDefault(x => Grid.GetRow(x) == row && Grid.GetColumn(x) == column);
if (day != null)
{
Button button = day.FindName("cellbutton") as Button;
if (button != null)
{
button.Content = "something";
}
}

How to retrieve data from a page in a frame on button click c#

private void btnEdit(object sender, RoutedEventArgs e)
{
}
in this button event i want to be able to retrieve two variables from a different xaml which is displayed in the frame of this window. I have no clue how to do this
I have a mainscreen with two buttons and a frame. in this frame i display a page. when this button in the above code is clicked i want to be able to get two variables from the pages textblock on the page. how would i go about this?
i basically need to collect data from the frame to be used in this event
I don't know that your project structure and what you try to do. So I would show you an example on the basis of I understood.
I created the below Page. Page name is "TestPage.xaml"
<Page x:Class="StackOverFlowAnswers.TestPage"
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:StackOverFlowAnswers"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
Title="TestPage">
<Grid>
<TextBlock x:Name="textBlock"/>
</Grid>
</Page>
I created MainWindow that has two button with one frame as below.
Now the source of the Frame is "TestPage.xaml".
<Window x:Class="StackOverFlowAnswers.MainWindow"
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:attached="clr-namespace:Parse.WpfControls.AttachedProperties"
xmlns:local="clr-namespace:StackOverFlowAnswers"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style x:Key="ConvertableTextBox" TargetType="TextBox">
<Setter Property="attached:CharacterConvertBehavior.ConvertEnable" Value="True"/>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" Grid.Row="0">
<Button Width="50" Margin="0 0 10 0" Click="Button_Click"/>
<Button Width="50" Margin="0 0 10 0"/>
</StackPanel>
<Grid Grid.Row="1">
<Frame x:Name="mainFrame" Source="TestPage.xaml">
</Frame>
</Grid>
</Grid>
</Window>
Now when the button is clicked would move to the event method of the below.
private void Button_Click(object sender, RoutedEventArgs e)
{
var page = (TestPage)this.mainFrame.Content;
page.textBlock.Text = "test!!!";
}
In the above way, you could access (retrieve) the var of the Page.

WPF - Trigger event if cursor is at certain position in image

I'm trying to trigger an event if the cursor is located in a certain part of an image in WPF. I thought I might try to "draw" an rectangle over the image and use a mousein/out event. I currently don't know how to accomplish this.
So for example I want to trigger a tooltip if the cursor enters the red rectangle of the image.
Currently I'm using the <Image></Image> Tag in WPF but that does not seem to work out....
Any help is appreciated!
I would probably utilize, that MouseEventArgs.GetPosition gives me the position relative to the object I hand over as an argument. Furthermore I would try to use the scaling of the image so that this approach is robust to later changing of the image size.
This approach would still give you a bunch of Magic Numbers in your code, but I don't think that can be avoided.
Here is a quick example showcasing what I mean.
Final result
Here is a gif showcasing the result of the approach, notice the
Yup this is an owl
appearing, when we are over a certain part of the image.
MainWindow.xaml
<Window x:Class="MousePosition.MainWindow"
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:local="clr-namespace:MousePosition"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid ShowGridLines="True" MouseMove="MousePosition">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="*"/>
<RowDefinition Height="100"/>
</Grid.RowDefinitions>
<TextBlock Grid.Column="1" x:Name="Feedback"/>
<Image x:Name="PrettyOwl" Grid.Column="1" Grid.Row="1" Source="Images/10-dithering-opt.jpg"/>
<TextBlock Grid.Column="1" Grid.Row="2" x:Name="WeAreThere"/>
</Grid>
MainWindow.xaml.cs
using System.Windows;
using System.Windows.Input;
namespace MousePosition
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MousePosition(object sender, MouseEventArgs e)
{
var scale = PrettyOwl.ActualWidth / PrettyOwl.Source.Width;
Point p = e.GetPosition(PrettyOwl);
Feedback.Text = string.Format("GetPosition(Mouse): X = {0}, Y = {1}", p.X, p.Y);
if (p.X > 100*scale && p.X < 200*scale && p.Y > 100*scale && p.Y < 200*scale)
{
WeAreThere.Text = "Yup, this is an owl";
}
e.Handled = true;
}
}
}
Hope that helps!
I thought I might try to "draw" an rectangle over the image and use a mousein/out event. I currently don't know how to accomplish this.
You could put the Image and an invisible element (with a background of Transparent) inside the same Grid:
<Grid>
<Image Source="screen.png" />
<Grid Background="Transparent" Width="10" Height="10" Margin="-40,-40,0,0"
MouseEnter="Grid_MouseEnter"
MouseLeave="Grid_MouseLeave"/>
</Grid>
The portion of the Image to be interactive is defined using the Margin property of invisible overlay element.

Changing content dynamically in wpf window

What i want to do is change/slide the content of a wpf window on the click of a button. I am new to wpf and have no clue how to do this. Please, if anyone can help me, I will be so grateful. Any video tutorials would be best.
You can put the content of the window into a UserControl. Your window then only has a content-control and a button to change the content. On a click on the button you can reassign the content-property of the content-control.
I've made a small example for this.
The XAML-Code for your MainWindow can look like this:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Content="Switch" Click="ButtonClick"/>
<ContentControl x:Name="contentControl" Grid.Row="1"/>
</Grid>
</Window>
I've added two UserControls to the solution. The CodeBehind for the MainWindow looks like:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.contentControl.Content = new UserControl1();
}
private void ButtonClick(object sender, RoutedEventArgs e)
{
this.contentControl.Content = new UserControl2();
}
}
Update
I've created a small usercontrol called MyUserControl. The xaml-markup looks like
<UserControl x:Class="WpfApplication.MyUserControl"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<StackPanel Orientation="Vertical">
<Label Content="This is a label on my UserControl"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<Button Content="Testbutton 1" Margin="5"/>
<Button Content="Testbutton 2" Margin="5"/>
</StackPanel>
<CheckBox Content="Check Me"/>
</StackPanel>
</UserControl>
In the button-click-event above you can assign a new instance of this usercontrol to the content-control. This you can do by:
this.contentControl.Content = new MyUserControl();

how can I enable scrollbars on the WPF Datagrid?

When I run the following Northwind WPF Toolkit Datagrid code from this article, I get a datagrid, but there are no scrollbars and hence the user can only see part of the datagrid. I am using the newest version March 2009.
What do I need to specify so that the WPF Datagrid has scrollbars?
I tried putting the datagrid in a ScrollViewer but that didn't help.
XAML:
<Window x:Class="TestDataGrid566.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="Window1" Height="600" Width="800">
<StackPanel>
<toolkit:DataGrid x:Name="TheDataGrid" AutoGenerateColumns="True"/>
</StackPanel>
</Window>
code-behind:
using System.Linq;
using System.Windows;
using TestDataGrid566.Model;
namespace TestDataGrid566
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
NorthwindDataContext db = new NorthwindDataContext();
var customers = from c in db.Customers
select c;
TheDataGrid.ItemsSource = customers;
}
}
}
Put the DataGrid in a Grid, DockPanel, ContentControl or directly in the Window. A vertically-oriented StackPanel will give its children whatever vertical space they ask for - even if that means it is rendered out of view.
WPF4
<DataGrid AutoGenerateColumns="True" Grid.Column="0" Grid.Row="0"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto">
</DataGrid>
with : <ColumnDefinition Width="350" /> & <RowDefinition Height="300" /> works fine.
Scrollbars don't show with <ColumnDefinition Width="Auto" /> & <RowDefinition Height="300" />.
Also works fine with: <ColumnDefinition Width="*" /> & <RowDefinition Height="300" />
in the case where this is nested within an outer <Grid>.
If any of the parent containers RowDefinition Height set to "Auto" also stoppers for scrollbars
Alternatively you may set Height "*"
Which happened in my case.
Adding MaxHeight and VerticalScrollBarVisibility="Auto" on the DataGrid solved my problem.
Add grid with defined height and width for columns and rows. Then add ScrollViewer and inside it add the dataGrid.
In my case I had to set MaxHeight and replace IsEnabled="False" by IsReadOnly="True"
This worked for me.
The key is to use * as Row height.
<Grid x:Name="grid">
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="*"/>
<RowDefinition Height="10"/>
</Grid.RowDefinitions>
<TabControl Grid.Row="1" x:Name="tabItem">
<TabItem x:Name="ta"
Header="List of all Clients">
<DataGrid Name="clientsgrid" AutoGenerateColumns="True" Margin="2"
></DataGrid>
</TabItem>
</TabControl>
</Grid>

Resources