Change object's layer dynamically in WPF - wpf

I need to do something like "Move up", "Move down" with the objects on my GRID from C# code while executing, is there any possibilities?

You can try this code:
private bool _isUp = false;
private void button1_Click(object sender, RoutedEventArgs e) {
if (_isUp) {
Canvas.SetZIndex(rectangle1, 1);
} else {
Canvas.SetZIndex(rectangle1, 0);
}
_isUp = !_isUp;
}
I just use 2 rectangles in my sample, for this.
<Rectangle Height="100" HorizontalAlignment="Left" Margin="68,142,0,0" Name="rectangle1" Stroke="Black" VerticalAlignment="Top" Width="200" Fill="#FF9D2A2A" />
<Rectangle Height="100" HorizontalAlignment="Left" Margin="10,120,0,0" Name="rectangle2" Stroke="Black" VerticalAlignment="Top" Width="200" Fill="#FF265D80" />

Related

WPF ListBox grow up on Event Handler?

I am extremely new using C# and that's far away from my mechanical skills ... I have very basic knowledge in C# but I am really willing to push the knowledge effort. Thanks to keep it in minde.
I creating some sort of application/ERP to help with working with my suppliers. The final goal is to create a list of suppliers, their skills, consult RFQ, update RFQ, send emails, ... I have been doing it in Excel VBA in the past but now I am willing to build up something stronger (and also willing to learn something new ...).
First of all, I am willing to create a ListBox with different kind of processes. I would like this ListBox to only show one ListBoxItems (one row) and when the mouse is entering into the ListBox, grow the ListBox to it's full size.
(My goal would be to ! create a ListBox with a dropdown for multiselection :=))
The dirty way I was thinking doing it is:
Create a TextBox with "Click to expand" text. When the mouse is entering the Textbox, hide it and change the listbox visibility to Visible (default visibility to Hidde);
Any thought about it ? Where should I start by ?
I will then selected multiple items in my ListBox.
Latter on, I would like to binding ListBox content to a data base (don't know how to create it on my computer).
Any help is greatly appreciated !
Cheers
Edit:
Below is my XAML code:
<Window x:Class="ERP_Fournisseurs.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:ERP_Fournisseurs"
mc:Ignorable="d"
Title="ERP Fournisseurs" Height="450" Width="800">
<Canvas>
<TabControl Height="369" Width="734" Canvas.Left="10" Canvas.Top="9">
<TabItem Header="Ajouter fournisseurs">
<Canvas Margin="0,0,0,-1">
<TextBlock Canvas.Left="10" Name="Fournisseur" TextWrapping="Wrap" Text="Nom fournisseur" Canvas.Top="16" />
<TextBox Height="23" Name="NomTXT" Canvas.Left="150" TextWrapping="Wrap" Text="Entrer nom fournisseur" Canvas.Top="14" Width="225" MouseDoubleClick="NomFournisseur_MouseDoubleClick" TextChanged="NomTXT_TextChanged" />
<TextBlock Canvas.Left="10" Name="Telephone" TextWrapping="Wrap" Text="Telephone" Canvas.Top="55"/>
<TextBox Height="23" Name="TelTXT" Canvas.Left="150" TextWrapping="Wrap" Text="Entrer téléphone fournisseur" Canvas.Top="55" Width="225" MouseDoubleClick="TelTXT_MouseDoubleClick"/>
<TextBlock Canvas.Left="10" Name="Mail" TextWrapping="Wrap" Text="Mail" Canvas.Top="84"/>
<TextBox Height="23" Name="MailTXT" Canvas.Left="150" TextWrapping="Wrap" Text="Entrer adresse e-mail" Canvas.Top="91" Width="225" MouseDoubleClick="MailTXT_MouseDoubleClick"/>
<TextBlock Canvas.Left="10" Name="Adresse" TextWrapping="Wrap" Text="Adresse" Canvas.Top="124"/>
<TextBox Height="23" Name="AdresseTXT" Canvas.Left="150" TextWrapping="Wrap" Text="Entrer adresse" Canvas.Top="127" Width="225" MouseDoubleClick="AdresseTXT_MouseDoubleClick"/>
<TextBlock Canvas.Left="10" Name="Types" TextWrapping="Wrap" Text="Types" Canvas.Top="157"/>
<TextBox Height="23" Name="TypesTXT" Canvas.Left="150" TextWrapping="Wrap" Text="Entrer types" Canvas.Top="162" Width="225" />
<TextBlock Canvas.Left="10" Name="Procédés" TextWrapping="Wrap" Text="Procédés" Canvas.Top="194"/>
<TextBlock Canvas.Left="10" Name="Matériaux" TextWrapping="Wrap" Text="Matériaux" Canvas.Top="232"/>
<TextBlock Canvas.Left="10" Name="Délais" TextWrapping="Wrap" Text="Délais" Canvas.Top="258"/>
<TextBlock Canvas.Left="10" Name="Autres" TextWrapping="Wrap" Text="Autres" Canvas.Top="289"/>
<TextBox Height="23" x:Name="AutreTXT" Canvas.Left="150" TextWrapping="Wrap" Text="Entrer types" Canvas.Top="289" Width="225" MouseDoubleClick="AutreTXT_MouseDoubleClick"/>
<Button Name="AjoutTXT" Content="Ajouter" Height="100" Width="75" Canvas.Left="563" Canvas.Top="107" Click="AjoutTXT_Click"/>
<ListBox Name="ListBoxProc" HorizontalAlignment="Left" VerticalAlignment="Top" Width="181" Height="186" Canvas.Left="409" Canvas.Top="54" Visibility="Hidden" MouseLeave="ListBoxProc_MouseLeave" MouseEnter="ListBoxProc_MouseEnter" SelectionMode="Multiple">
<ListBoxItem>TEST 1</ListBoxItem>
<ListBoxItem>TEST 1</ListBoxItem>
<ListBoxItem>TEST 1</ListBoxItem>
<ListBoxItem>TEST 1</ListBoxItem>
</ListBox>
<TextBox Height="23" Name="ProcTXT2" Canvas.Left="150" TextWrapping="Wrap" Text="Entrer types" Canvas.Top="196" Width="225" />
</Canvas>
</TabItem>
<TabItem Header="Suivi devis" >
<Canvas>
</Canvas>
</TabItem>
<TabItem Header="Liste fournisseurs" >
<Canvas>
</Canvas>
</TabItem>
</TabControl>
</Canvas>
</Window>
Below is my XAML.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ERP_Fournisseurs
{
/// <summary>
/// Logique d'interaction pour MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void NomFournisseur_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
NomTXT.Text = "";
}
private void TelTXT_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
TelTXT.Text = "";
}
private void MailTXT_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
MailTXT.Text = "";
}
private void AdresseTXT_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
AdresseTXT.Text = "";
}
private void AjoutTXT_Click(object sender, RoutedEventArgs e)
{
}
private void AutreTXT_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
AutreTXT.Text = "";
}
private void ProcTXT_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void NomTXT_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void MaterTXT_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
private void ProcTXT_MouseEnter(object sender, MouseEventArgs e)
{
}
private void ListBoxProc_MouseEnter(object sender, MouseEventArgs e)
{
ListBoxProc.Visibility = Visibility.Hidden;
}
private void ListBoxProc_MouseLeave(object sender, MouseEventArgs e)
{
ListBoxProc.Visibility = Visibility.Visible;
}
//https://www.c-sharpcorner.com/UploadFile/mahesh/listbox-in-wpf/ Binding ListBox to Data Base
}
}
What I have tried:
Asked google my solution, but it's answerless;
Tried the following code (https://www.codeproject.com/Questions/696480/How-to-make-a-ListBox-grow-on-mouseover) but not working

How control re-position depends on other control visibility in same panel

I have two buttons inside a stack panel. Initially B1 button is on top, then B2. I will change button visibility dynamically from code so that, when I change B1 visibility hidden, then B2 will come on top. How can I achieve this functionality.
<Grid>
<StackPanel >
<Button Content="B1" Height="20" Width="100" Visibility="Visible"/>
<Button Content="B2" Height="20" Width="100" Visibility="Visible"/>
</StackPanel>
</Grid>
First you remove the Statckpanel and put then in a Grid and you can achieve
Try something like this.
<Grid>
<Button Content="B1" Height="20" Width="100" Visibility="Visible" Click="Button_Click" x:Name="B1" />
<Button Content="B2" Height="20" Width="100" Visibility="Visible" x:Name="B2" Click="B2_Click" />
</Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
B1.Visibility = System.Windows.Visibility.Hidden;
B2.Visibility = System.Windows.Visibility.Visible;
}
private void B2_Click(object sender, RoutedEventArgs e)
{
B2.Visibility = System.Windows.Visibility.Hidden;
B1.Visibility = System.Windows.Visibility.Visible;
}
This should give you similar behaviour. Change according to your use

Animate control with respecting parents/container

I have a control that I move with help of animation from the bottom to the final position. My problem is now that I want to change the behaviour of the animation, so that it respects the outer container (DarkGray).
The orange Ractangle should only be visible on the white background and not on the darkgray Grid!
Code:
MainWindow.xaml:
<Grid Background="DarkGray">
<Grid Margin="50"
Background="White">
<Rectangle x:Name="objectToMove"
VerticalAlignment="Bottom"
Fill="Orange"
Height="50"
Width="50"/>
<Button Height="20"
Width="40"
Margin="20"
Content="Move"
Click="Button_Click"
VerticalAlignment="Top"/>
</Grid>
</Grid>
MainWindow.xaml.cs:
private void Button_Click(object sender, RoutedEventArgs e)
{
var target = objectToMove;
var heightOfControl = target.ActualHeight;
var trans = new TranslateTransform();
target.RenderTransform = trans;
var myAnimation = new DoubleAnimation(heightOfControl, 0, TimeSpan.FromMilliseconds(600));
trans.BeginAnimation(TranslateTransform.YProperty, myAnimation);
}
Current:
Desired solution:
Use ClipToBounds Property for this.
<Grid Margin="50"
Background="White"
ClipToBounds="True">

How to change data in a Silverlight DataContext Object

I have a datacontext object that displays a list of uploaded files, and number of copies. So far, the user can click an edit icon that is in each row, and retrieve the data in that row, but I do not know how to change any data.
Here is the button event for the 'change details' dialog on the main page:
private void Image_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
{
Image b = sender as Image;
FileReceivedItem fi = b.DataContext as FileReceivedItem;
string fileCopies = fi.fileCopies;
string fileName = fi.FileName;
DetailsWindow detailsDlg = new DetailsWindow(fileCopies, fileName);
detailsDlg.Show();
detailsDlg.Closed += new EventHandler(detailsWnd_Closed);
}
public DetailsWindow detailsDlg;
void detailsWnd_Closed(object sender, EventArgs e)
{
MessageBox.Show("I want to change fileCopies here");
}
The relevant portions of the XAML:
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Hidden" ItemsSource="{StaticResource FileReceivedItem}" Name="filesReceivedList" HorizontalAlignment="Left" Grid.Row="1" Grid.Column="1" Background="White" Margin="12,23,0,0"
VerticalAlignment="Top" Width="411" Height="175" Loaded="filesReceivedList_Loaded">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Width="404" Height="19" Orientation="Horizontal" >
<TextBlock x:Name="ListFileName" Text="{Binding FileName}" Width="283" VerticalAlignment="Center">
<ToolTipService.ToolTip>
<ToolTip Content="{Binding FileName}"></ToolTip>
</ToolTipService.ToolTip>
</TextBlock>
<TextBlock Text="{Binding fileCopies}" Width="50" VerticalAlignment="Center"/>
<TextBlock Text="{Binding FileID}" Width="0" Visibility="Collapsed" VerticalAlignment="Center"/>
<Image Source="images/Edit.png" MouseLeftButtonUp="Image_MouseLeftButtonUp_1" Width="20" Margin="0 0 0 0">
<ToolTipService.ToolTip>
<ToolTip Content="Edit"></ToolTip>
</ToolTipService.ToolTip>
</Image>
<TextBlock Text="" Width="10" />
<Image Source="images/Delete.png" MouseLeftButtonUp="Image_MouseLeftButtonUp" >
<ToolTipService.ToolTip>
<ToolTip Content="Delete"></ToolTip>
</ToolTipService.ToolTip>
</Image>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Let me know if any important pieces of code are missing. I'm not sure how the data source works exactly.
I eventually got it to work. Pretty straightforward. I just haven't used SL in ahwile. This is what I did, although there might be a better way that someone might provide as a better answer:
void detailsWnd_Closed(object sender, EventArgs e)
{
changeFileCopies(detailsDlg.FileName, detailsDlg.nCopies.Text);
}
public void changeFileCopies(string filename, string newCopies)
{
var row = (FileReceivedItem)filesReceivedDataSource.Where(x => x.FileName == filename).FirstOrDefault();
row.fileCopies = newCopies;
filesReceivedList.ItemsSource = null;
this.filesReceivedList.ItemsSource = filesReceivedDataSource;
}

move canvas on key down

I want to move canvas right when I press ->.
I set event KeyDown and this is method for event
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key==Key.Right)
{
}
}
But what to write inside if?Canvas.setLeft doesn't work
<Canvas x:Name="totle" KeyDown="Window_KeyDown">
<Ellipse Name="yio" Canvas.Left="40" Canvas.Top="40" Height="30" Width="30" Fill="Beige"/>
<Line X1="40" Canvas.Left="67" Canvas.Top="51" StrokeThickness="40" Stroke="Red" Height="10" Width="45" Fill="#FFD86464" OpacityMask="Red" />
<Rectangle Canvas.Left="20" Canvas.Top="70" Width="70" Height="20" Fill="Beige"/>
</Canvas>
To achieve this effect, first use a RenderTransform and name it canvasTranform:
<Canvas x:Name="totle" KeyDown="Window_KeyDown">
<Canvas.RenderTransform>
<TranslateTransform x:Name="canvasTransform" />
</Canvas.RenderTransform>
<Ellipse Name="yio" Canvas.Left="40" Canvas.Top="40" Height="30" Width="30" Fill="Beige"/>
<Line X1="40" Canvas.Left="67" Canvas.Top="51" StrokeThickness="40" Stroke="Red" Height="10" Width="45" Fill="#FFD86464" OpacityMask="Red" />
<Rectangle Canvas.Left="20" Canvas.Top="70" Width="70" Height="20" Fill="Beige"/>
</Canvas>
Then, in your event handler, reference the TranslateTransform by name and set the X property:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Right)
{
canvasTransform.X = 100;
}
}
If you the transform to be additive (the canvas keeps moving right as you press the right arrow), add to the X property each time:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Right)
{
canvasTransform.X += 100;
}
}

Resources