Drag N drop images in a viewbox - wpf

I have a usercontrol with 6 viewbox, each can have an image. I have not set the width or height of the usercontrol(not even the viewboxes). At first the viewboxes are empty.I can add the images dynamically.
<Grid x:Name="DashBoardGrid" DockPanel.Dock="Right">
<Grid.RowDefinitions>
<RowDefinition Height="20"></RowDefinition>
<RowDefinition Height="200*"></RowDefinition>
<RowDefinition Height="10"></RowDefinition>
<RowDefinition Height="200*"></RowDefinition>
<RowDefinition Height="20"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"></ColumnDefinition>
<ColumnDefinition Width="200*"></ColumnDefinition>
<ColumnDefinition Width="10"></ColumnDefinition>
<ColumnDefinition Width="200*"></ColumnDefinition>
<ColumnDefinition Width="10"></ColumnDefinition>
<ColumnDefinition Width="200*"></ColumnDefinition>
<ColumnDefinition Width="20"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Border Grid.Row="1" Grid.Column="1">
<DockPanel x:Name="dockPanel1" Margin="10,30,10,10" >
<TextBlock x:Name="TitleTxtblk1" DockPanel.Dock="Top" FontSize="10pt" TextWrapping="Wrap">screen 1</TextBlock>
<Viewbox x:Name="Viewbox1" MouseDown="Viewbox_MouseDown" MouseEnter="ScaleUp" MouseLeave="ScaleDown"
PreviewMouseLeftButtonDown="DockPanel_PreviewMouseLeftButtonDown" PreviewMouseMove="dockPanel_MouseMove"
Drop="dockPanel_Drop" DragEnter="dockPanel_DragEnter" AllowDrop="True">
</Viewbox>
</DockPanel>
</Border>
<Border Grid.Row="1" Grid.Column="3" >
<DockPanel x:Name="dockPanel2" Margin="10,30,10,10" >
<TextBlock x:Name="TitleTxtblk2" DockPanel.Dock="Top" FontSize="10pt" TextWrapping="Wrap">screen 2</TextBlock>
<Viewbox x:Name="Viewbox2" MouseDown="Viewbox_MouseDown" MouseEnter="ScaleUp" MouseLeave="ScaleDown"
PreviewMouseLeftButtonDown="DockPanel_PreviewMouseLeftButtonDown" PreviewMouseMove="dockPanel_MouseMove"
Drop="dockPanel_Drop" DragEnter="dockPanel_DragEnter" AllowDrop="True">
</Viewbox>
</DockPanel>
</Border>
I want to drag and drop the images from one viewbox to any other empty viewbox. But when the viewbox is empty, it is not visible at all. because it does not have a height or width. So i am not able to drop the image on it.
So what will i have to to such that when any one view box has an image all the view box have the same height and width? any other solution is welcome.

You could handle the drag/drop events in a parent control like the Border. And then you probably need a reference to your viewbox in your drag/drop methode. Try this:
<Border Tag="{Binding ElementName=Viewbox2,Path=.} … >
and in your drag/drop methode
ViewBox v = (sender as Border).Tag as ViewBox;

Related

Layout problem with autogenerated DataGrid in WPF/XAML

I've got a problem with WPF/XAML. I can't figure out how to let the left Grid (with a centered image inside and backgroud color) grow its height, depending on the autogenerated height on the datagrid on the right.
They both are presented in a global grid. If I set a fixed height on the corresponding row, the autogenerated datagrid shows a cruel vertical line, depending on the fixed row-height I set before.
layout with row-height on auto
layout with fixed row-height, but with ugly line at the bottom
<!--ElementR0C1-->
<Grid Name="test">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<!--Width ElementsC1-->
<ColumnDefinition Width="160"/>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="500"/>
</Grid.ColumnDefinitions>
<!--START-->
<Label Grid.Row="0" Grid.Column="0" Style="{StaticResource L1}" VerticalAlignment="Top">Process</Label>
<Grid Grid.Row="1" Grid.Column="0" Background="DDDDDD">
<Image Margin="0,30,0,0" Height="64" Width="64" Source="../../Resources/ok.png" Name="imgJobs" VerticalAlignment="Center" HorizontalAlignment="Center"/>
</Grid>
<DataGrid x:Name="grdJobs" Grid.Row="0" Grid.Column="2" Grid.RowSpan="2" Width="500">
-- some stuff here
</DataGrid>
</Grid>

Fixed header and footer outside scrollview

I have a grid that has a header, content and a footer:
<Grid.RowDefinitions>
<RowDefinition Height="55"/> <!--HEADER-->
<RowDefinition Height="*"/> <!--CONTENT-->
<RowDefinition Height="55"/> <!--FOOTER-->
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<Rectangle Grid.Row="0" Grid.ColumnSpan="3"></Rectangle>
<ScrollViewer Grid.Row="1" Grid.Column="1" >
<Grid>
...
</Grid>
</ScrollViewer>
<Rectangle Grid.Row="3" Grid.ColumnSpan="3"></Rectangle>
</Grid>
I want to have it so that the header and footer are fixed and the content is scrollable. Within the second nested grid there is a lot of content, hence the scroll view. When I run the application, the scrollviewer still scrolls with the header and footer! I can't figure out what I'm doing wrong, is there a better layout I should be using?
Please let me know! I'd rather not use C#.
Thanks!
Unless you are doing something strange either around the XAML you have posted or inside the nested Grid object, your XAML works as you intended.
I very slightly modified your XAML, just to visibly show your header and footer, and to add some content to your inner grid.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="55"/><!--HEADER-->
<RowDefinition Height="*"/><!--CONTENT-->
<RowDefinition Height="55"/><!--FOOTER-->
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<Rectangle Grid.Row="0" Grid.ColumnSpan="3" Height="55" Fill="Red"/>
<ScrollViewer Grid.Row="1" Grid.Column="1" >
<Grid >
<ListBox ItemsSource="{Binding Path=myItems, FallbackValue='123456789abcdefghijklmno'}"/>
</Grid>
</ScrollViewer>
<Rectangle Grid.Row="3" Grid.ColumnSpan="3" Height="55" Fill="Blue"/>
</Grid>
Shown below is the result. Your ScrollViewer scrolls just fine while leaving the header and footer in place.
I'm not sure what else you have going on in your window, but this is the only XAML in the window I used for testing, and it works perfectly. As a note, I limited the height of the window to '400' so the inner grid did not continue to grow since it's height was set to *. You can achieve the same result by setting a maximum height on your outer Grid.

WPF: auto resize ListView width according Window size

So i have this ListView inside TabControl:
<TabControl>
<TabItem Width="70" Height="70" Margin="0,0,0,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1">
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1">
<ListView>
...
</ListView>
</Grid>
<Grid>
</Grid>
</Grid>
</TabItem>
</TabControl>
My window ResizeMode is CanResizeWithGrip and when i resize my application i want my ListView to auto resize to according my Window width.
I try to define my ListView HorizontalAlignment Stretch bu when my application width changed my ListView with not.
I believe since your ListView is nested within other controls, those controls would need their HorizontalAlignment set to Stretch. I'm on my phone right now so I'll try out your XAML on my system when I get home and update my answer if I find something else is the culprit.
Edit: I've copied your code into VS and its stretching properly for me. I don't have any contents to check but if I set
<TabControl>
<TabItem Width="70" Height="70" Margin="0,0,0,0">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="10"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="1">
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1">
<ListView Background="Black">
</ListView>
</Grid>
</Grid>
</Grid>
</Grid>
</TabItem>
</TabControl>
I can see that the control is in fact stretching when I resize the window. Now I'm unsure of what the issue you are running into is. Any chance you could post more details?

How to have TextBlocks with same scale?

I have a problem trying to adjust the size of TextBlocks. I have a grid with 3 columns and 6 rows, in the second column I have a TextBlock in each row, each TextBlock text has different length so when I resize the window some texts looks larger or smaller than the others.
Is there a way to have the same scale in each TextBlock when I resize the window? I would like all the TextBlock look the same size when I resize the window.
This is the code I have:
<Grid Name="grid">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Viewbox Grid.Column="1">
<TextBlock x:Name="txt1" Text="Some long text 12345" FontSize="20" />
</Viewbox>
<Viewbox Grid.Column="1" Grid.Row="1">
<TextBlock x:Name="txt2" Text="Some longer text 1234567890" FontSize="20" />
</Viewbox>
<Viewbox Grid.Column="1" Grid.Row="2">
<TextBlock x:Name="txt3" Text="Text3" FontSize="20" />
</Viewbox>
<Viewbox Grid.Column="1" Grid.Row="3">
<TextBlock x:Name="txt4" Text="Text4" FontSize="20" />
</Viewbox>
<Viewbox Grid.Column="1" Grid.Row="4">
<TextBlock x:Name="txt5" Text="Text5" FontSize="20" />
</Viewbox>
<Viewbox Grid.Column="1" Grid.Row="5">
<TextBlock x:Name="txt6" Text="Text6" FontSize="20" />
</Viewbox>
Thanks in advance,
Alberto
Place the TextBlock directly in the gird, no need of viewBox
Set instead of *
There is one more option you can set in ColumnDefination or RowDefination "SharedSizeGroup"
the rows or columns with same name will share the same height or width

How to organize elements inside a Button

i'm new to wpf .
i got a wpf button ,
in it i need to place 2 elements a textblock and a viewbox encapsulating a canvas
the problem is i can't seem to see the canvas at all , unless i give it static values for its size
<Button Margin="10,30,10,10" Padding="0">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Me" Grid.Row="0" Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"></TextBlock>
<Viewbox Margin="0,0,0,5">
<Canvas Background="red" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.Row="0" Grid.Column="1" >
<Ellipse HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stroke="Black" StrokeThickness="4" ></Ellipse>
</Canvas>
</Viewbox>
</Grid>
</Button>
iv'e also attempted this using a stack panel with an horizontal orintation
in any case the canvas does not show
any thoughts of what i'm doing wrong ?
thanks.
Attached Grid properties only work when at the Grid-child-level, i.e. the properties set on the canvas do not take effect they should be set on the container, the ViewBox which is a direct child of the Grid.
Viewboxes only work if the content has a concrete size, you probably need neither the ViewBox nor the Canvas. If you want the Ellipse to be a circle set Stretch="Uniform"
The contents of Buttons do not stretch by default you should set HorizontalContentAligment and its vertical counterpart to Stretch.
e.g.
<Button Margin="10,30,10,10" Padding="0" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<TextBlock Text="Me" Grid.Row="0" Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"></TextBlock>
<Ellipse Stretch="Uniform" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
Stroke="Black" StrokeThickness="4"></Ellipse>
</Grid>
</Button>

Resources