In my WPF project, I have a Canvas in which I do some drawings. Following the code for the Window:
<Window x:Name="PropertyDefinition_Window" x:Class="PushOverStraus7.PropertyDefinitionWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="PropertyDefinitionWindow" Height="360" Width="730" Loaded="Window_Loaded">
<Grid>
<TreeView SelectedItemChanged="Treeview_PropertyDefinition_SelectedItemChanged">
<TreeViewItem IsExpanded="True">
<TreeViewItem />
</TreeViewItem>
<TreeViewItem>
<TreeViewItem"/>
</TreeViewItem>
</TreeView>
<Canvas x:Name="Canvas_LongitudinalBarRectanglePage" HorizontalAlignment="Left" Height="251" Margin="255,10,0,0" VerticalAlignment="Top" Width="285" Background="Black">
</Canvas>
<Button x:Name="NewButton" Content="Nuovo" HorizontalAlignment="Left" Margin="22,277,0,0" VerticalAlignment="Top" Width="45" Click="NewButton_Click"/>
<Button x:Name="DuplicateButton" Content="Duplica" HorizontalAlignment="Left" Margin="85,277,0,0" VerticalAlignment="Top" Width="45"/>
</Grid>
I want that, resizing the Window, the Canvas size changes accordingly. Then, I need to have ActualWidth and ActualHeight of the Canvas resized to use them for drawing alghorithm.
How can I have the automatical resize of the Canvas and how to get values described above?
Thanks for your help
EDIT:
I've changed the question as asked
With the following XAML the Canvas will automatically be resized when the Window size changes.
<Window ...>
<Frame>
<Frame.Content>
<Page>
<Canvas>
...
</Canvas>
</Page>
</Frame.Content>
</Frame>
</Window>
Related
I have a WPF Window with a DockPanel that contains a Menu, a TabControl, and a Status Bar. I can get the Menu to dock to the top and the Status Bar to dock to the bottom, but I can't get the Tab control to fill the area between those two controls.
<Window x:Class="MediaCatalog.Window1"
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:MediaCatalog"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">
<DockPanel LastChildFill="False">
<Menu Width="Auto" VerticalAlignment="Top" HorizontalAlignment="Left"
DockPanel.Dock="Top">
</Menu>
<TabControl DockPanel.Dock="Top" Margin="0,0,0,0"
BorderThickness="1,1,1,1" Height="291">
</TabControl>
<StatusBar DockPanel.Dock="Bottom" Height="22">
<StatusBar/>
</StatusBar>
</DockPanel>
The last element will fill the DockPanel unless you set LastChildFill to false:
<DockPanel>
<Menu DockPanel.Dock="Top">
<MenuItem Header="A" />
<MenuItem Header="B" />
</Menu>
<StatusBar DockPanel.Dock="Bottom" Height="22" />
<TabControl Margin="0,0,0,0" BorderThickness="1,1,1,1">
<TabItem Header="A" />
<TabItem Header="B" />
</TabControl>
</DockPanel>
Note that you shouldn't set the DockPanel.Dock attached property of the last element.
Also, you don't want to specify a default Height for the TabControl if you want it to fill the remaining space.
The above sample markup will make the TabControl fill the remaining space between the top Menu and the bottom StatusBar:
I want to display a large image in a Window where the Image will have scroll bars if it too big for the area of the screen it is in.
Underneath the image I want a button panel. For this I have put the Image inside a ScrollViewer in a DockPanel that contains a StackPanel to contain the Buttons in the Bottom part. The idea is to click the Browse button to set the image (from code behind handling Button Click)
The following example I put together will just keep the image size (2144 x 1424) and I cannot see the lower button panel.
<Window x:Class="WpfIssues.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:WpfIssues"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DockPanel>
<TextBlock Text="Test Image" FontSize="30" DockPanel.Dock="Top"></TextBlock>
<StackPanel Orientation="Vertical">
<DockPanel x:Name="PhotoPanel">
<StackPanel DockPanel.Dock="Bottom" Orientation="Horizontal" HorizontalAlignment="Right">
<Button Click="Button_Click">Browse...</Button>
</StackPanel>
<ScrollViewer
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<StackPanel>
<Image x:Name="PhotoImage"
Stretch="None"
Source="Resources/bear grills.png"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</StackPanel>
</ScrollViewer>
</DockPanel>
</StackPanel>
</DockPanel>
</Grid>
</Window>
I cant figure out why.
Try putting the button before the scrollviewer, like this:
<DockPanel>
<TextBlock Text="Test Image" FontSize="30" DockPanel.Dock="Top" />
<Button Content="Browse..." DockPanel.Dock="Bottom" />
<ScrollViewer
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<Image x:Name="PhotoImage"
Stretch="None"
Source="http://images6.fanpop.com/image/photos/33600000/Bear-Grylls-bear-grylls-33656894-3504-2336.jpg"
HorizontalAlignment="Center"
VerticalAlignment="Center" />
</ScrollViewer>
</DockPanel>
I have a ComboBox within a tab and I can change its size, skew and rotation with the mouse. However, when I want to move it around, I'm not allowed to. To change the combobox's position, I have to manually enter the coordinates in the margin fields, which is really annoying. Why can't I simply move it by dragging it with the mouse?
UPDATE
This actually happens only in a second tab. In the first tab I can move around controls like expected.
So I cut&pasted the tab part in my xaml file in order to change the tab order. Now, I can move around controls in the first tab (former 2nd tab) whereas I can't move controls in the 2nd tab.
Sounds like a WPF designer bug to me...
UPDATE 2
This is a simple test case. The TestComboBox in the 2nd tab can't be moved.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="718" Width="728" Background="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}">
<TabControl HorizontalAlignment="Left" VerticalAlignment="Top">
<TabItem Header="TabItem">
<Grid Margin="0,10,0,4" Height="639" Width="708">
</Grid>
</TabItem>
<TabItem Header="TabItem" Height="23">
<Grid Margin="0,10,0,4" Height="639" Width="708">
<ComboBox x:Name="TestComboBox" HorizontalAlignment="Left" Margin="84,10,0,0" Width="217" VerticalAlignment="Top" Height="22"/>
</Grid>
</TabItem>
</TabControl>
</Window>
After changing the tab order, TestComboBox can be moved:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="718" Width="728" Background="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}">
<TabControl HorizontalAlignment="Left" VerticalAlignment="Top">
<TabItem Header="TabItem" Height="23">
<Grid Margin="0,10,0,4" Height="639" Width="708">
<ComboBox x:Name="TestComboBox" HorizontalAlignment="Left" Margin="84,10,0,0" Width="217" VerticalAlignment="Top" Height="22"/>
</Grid>
</TabItem>
<TabItem Header="TabItem">
<Grid Margin="0,10,0,4" Height="639" Width="708">
</Grid>
</TabItem>
</TabControl>
</Window>
I had the same problem. Solved it by placing the the TabControl inside a grid - see code below.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="718" Width="728" Background="{DynamicResource {x:Static SystemColors.ControlLightBrushKey}}">
<Grid>
<TabControl HorizontalAlignment="Left" VerticalAlignment="Top">
<TabItem Header="TabItem" Height="23">
<Grid Margin="0,10,0,4" Height="639" Width="708">
<ComboBox x:Name="TestComboBox" HorizontalAlignment="Left" Margin="84,10,0,0" Width="217" VerticalAlignment="Top" Height="22"/>
</Grid>
</TabItem>
<TabItem Header="TabItem">
<Grid Margin="0,10,0,4" Height="639" Width="708">
<ComboBox x:Name="TestComboBox2" HorizontalAlignment="Left" Margin="84,10,0,0" Width="217" VerticalAlignment="Top" Height="22"/>
</Grid>
</TabItem>
</TabControl>
</Window>
I just tried adding a TabControl into a new WPF Application. I added two TabItem controls with a ComboBox in each. At first, Visual Studio allowed me to move the ComboBox in the first tab, but not the second.
After I moved the ComboBox in the second tab, it would jump back to its original position when I let go of the mouse button. On closer inspection, this was because there was a Grid in the first TabItem, but not the second... perhaps you had a similar problem?
However, after testing the code that you just added, I'm afraid to say that I don't have the same problem that you do. Perhaps you should restart Visual Studio and maybe even your computer?
I have the same problem while working with WPF, but i do this to "bypass" it.
Just comment the grids before the one you'll be working on.
I know that that is painfull to do when working with large projects, but it's the only way i have found.
In a WPF project, I want to dock a DataGrid to the bottom of a window so that if the window resizes, I will be able to utilize more of the DataGrid. Like this:
How do I do that? All my DockPanel attempts have failed.
The current attempt is here:
<Window x:Class="Foo.SQLDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:Foo.Controls"
Title="test" ResizeMode="CanResize" Width="400" Height="400">
<StackPanel Orientation="Vertical" Height="Auto" Width="Auto">
<StackPanel Orientation="Vertical">
<Label Content="SQL" HorizontalAlignment="Left"/>
<TextBox Width="377" Height="100" Name="txtSQL"/>
<Button Content="Run SQL" Click="Button_Click_1" />
</StackPanel>
<Label Content="Result" HorizontalAlignment="Left"/>
<ScrollViewer Width="Auto" Height="180" DockPanel.Dock="Right,Bottom"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto">
<DataGrid x:Name="dataResult" />
</ScrollViewer>
</StackPanel>
</Window>
The height of the scrollviewer+datagrid will not adapt however.
First of all, using DockPanel.Dock without having a DockPanel as a parent doesn't do much...
In my example I changed your root StackPanel to a DockPanel so it will work as you want.
I also used DockPanel.LastChildFill property which makes sure the last child of the DockPanel will get all the remaining space:
<DockPanel LastChildFill="True">
<StackPanel Orientation="Vertical" DockPanel.Dock="Top">
<Label Content="SQL" HorizontalAlignment="Left"/>
<TextBox Width="377" Height="100" Name="txtSQL"/>
<Button Content="Run SQL" Click="Button_Click_1" />
</StackPanel>
<Label Content="Result" HorizontalAlignment="Left" DockPanel.Dock="Top"/>
<ScrollViewer DockPanel.Dock="Bottom,Right"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto">
<DataGrid x:Name="dataResult" />
</ScrollViewer>
</DockPanel>
Finally, to make it really stretch on all the remaining space, I removed the Height property you set, as this blocked it from stretching.
Not sure if useful or if i understand you question the right way but have you tried this:
<DataGrid DockPanel.Dock="Right, Bottom" VerticalAlignment="Bottom" HorizontalAlignment="Right" ></DataGrid>
I have a StackPanel and for some reason I can't get the content of the ScrollViewer on the very bottom to be scrollable, despite the fact that the internal height of the Frame clearly exceeds the bounds of the ScrollViewer. I previously had a Grid as the root container of the Window, but when I changed it to StackPanel the scrollbar no longer appears.
The only thing that seems to work is if I explicitly set the height of the ScrollViewer, but then it does not size with thw Window when it's resized.
Do I have to use a Grid?
Sorry if this is obvious; I'm relatively new to WPF.
Thanks in advance for any help!
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
d:DesignWidth="566" Width="719" >
<StackPanel VerticalAlignment="Stretch" >
<Menu HorizontalAlignment="Stretch" Name="Menu1" VerticalAlignment="Stretch" Width="Auto">
<MenuItem Header="_File" Name="MenuItem1">
<MenuItem Header="_Print" Name="MenuItem2" />
</MenuItem>
</Menu>
<Canvas x:Name="SearchCanvas" Width="681" Height="55">
<ComboBox Canvas.Left="6" Canvas.Top="0" Height="22" x:Name="cbLookupField" Width="302" Text="" SelectedIndex="0">
<ComboBoxItem Content="Reference Name" />
<ComboBoxItem Content="Matter" />
<ComboBoxItem Content="Client Loan Number" />
</ComboBox>
<TextBox KeyDown="tbLookup_KeyDown" Canvas.Left="6" Canvas.Top="28" Height="23" x:Name="tbLookup" Width="302" />
<Button Canvas.Left="314" Canvas.Top="27" Content="Search" Height="24" Name="btnSearch" Width="106" />
<ListView MouseDoubleClick="lvSearchResults_MouseDoubleClick"
ItemsSource="{Binding Tables[0]}"
Canvas.Left="-8" Canvas.Top="57" Height="129" Name="lvSearchResults" Width="697" Visibility="Hidden">
<ListView.View>
<GridView x:Name="gvResultsGridView"/>
</ListView.View>
</ListView>
</Canvas>
<ScrollViewer x:Name="ScrollViewer1" VerticalScrollBarVisibility="Visible">
<Frame Height="Auto" Source="Full Report.xaml" VerticalAlignment="Stretch" x:Name="Frame1" />
</ScrollViewer>
</StackPanel>
</Window>
The ScrollViewer in the bottom does not have a fixed height, and therefore because it's contained in a StackPanel, its height will be the height of the content, which in this case is the Frame height.
You can either set a fixed height for the ScrollViewer, or use a Grid instead of a StackPanel. In case you use a grid you should set a fixed height for the row in which the ScrollViewer will be located.
Hope it helps!