I have the following user control xaml, containing multiple RichTextBox controls:
<UserControl x:Class="Organizer.UserControls.RowViewUserControl"
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:Organizer.UserControls"
xmlns:viewmodels="clr-namespace:Organizer.ViewModels"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800"
x:Name="ucRow">
<DockPanel Margin="2 2 2 2" Grid.Row="2" Grid.Column="1">
<RichTextBox x:Name="RTBMinus5" Margin="1 0 0 3" DockPanel.Dock="Top" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
<RichTextBox x:Name="RTBMinus4" Margin="1 0 0 3" DockPanel.Dock="Top" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
<RichTextBox x:Name="RTBMinus3" Margin="1 0 0 3" DockPanel.Dock="Top" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
<RichTextBox x:Name="RTBMinus2" Margin="1 0 0 3" DockPanel.Dock="Top" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
<RichTextBox x:Name="RTBMinus1" Margin="1 0 0 3" DockPanel.Dock="Top" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
<RichTextBox x:Name="RTBPlus5" Margin="1 0 0 3" DockPanel.Dock="Bottom" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
<RichTextBox x:Name="RTBPlus4" Margin="1 0 0 3" DockPanel.Dock="Bottom" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
<RichTextBox x:Name="RTBPlus3" Margin="1 0 0 3" DockPanel.Dock="Bottom" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
<RichTextBox x:Name="RTBPlus2" Margin="1 0 0 3" DockPanel.Dock="Bottom" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
<RichTextBox x:Name="RTBPlus1" Margin="1 0 0 3" DockPanel.Dock="Bottom" Height="{Binding RowHeightSliderValue}" BorderBrush="Gray" Background="Black" Foreground="Gray" FontSize="14"/>
<RichTextBox Name="RTBSelected" Margin="1 0 0 3" BorderBrush="Purple" Background="Black" Foreground="White" FontSize="18" />
</DockPanel>
</UserControl>
... and the following Code Behind where i define the method ShowTextInRow(string textstring) which can be called from somewhere else in the program to take a text string and display it into the RichTextBox named "RTBSelected":
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace Organizer.UserControls
{
public partial class RowViewUserControl : UserControl
{
public RowViewUserControl()
{
InitializeComponent();
}
public static void ShowTextInRow(string textstring)
{
// Source: https://stackoverflow.com/questions/15983278/storing-data-of-rich-text-box-to-database-with-formatting
byte[] byteArray = Encoding.ASCII.GetBytes(textstring);
using (MemoryStream ms = new MemoryStream(byteArray))
{
TextRange tr = new TextRange(RTBSelected.Document.ContentStart, RTBSelected.Document.ContentEnd);
tr.Load(ms, DataFormats.Rtf);
}
}
}
}
I am stuck with the following problem: if i declare the "ShowTextInRow()" method as static then the codebehind does not recognize the reference to the RichTextBox by its name "RTBSelected". On the other hand, if i remove the 'static' declaration from the "ShowTexdInRow()" method, the codebehind seems to recognize the RichTextBox by its name "RTBSelected" but i can no longer call this method from elsewhere in my program.
Sorry if i missed something very basic - i am new to C#/WPF. Thanks in advance for the support.
Because this operation is part of a UserControl you would not want to call it static. Calling it static would give you other errors as you would attempt to load into a RichTextBox that doesn't exist. The UserControl must be initialized before you do anything with it.
Remove the static declaration and call it from other areas of your program by initializing your UserControl first.
Static declaration removed:
public void ShowTextInRow(string textstring)
{
// Source: https://stackoverflow.com/questions/15983278/storing-data-of-rich-text-box-to-database-with-formatting
byte[] byteArray = Encoding.ASCII.GetBytes(textstring);
using (MemoryStream ms = new MemoryStream(byteArray))
{
TextRange tr = new TextRange(RTBSelected.Document.ContentStart, RTBSelected.Document.ContentEnd);
tr.Load(ms, DataFormats.Rtf);
}
}
Calling from other areas within the namespace:
RowViewUserControl rvuc = new RowViewUserControl();
rvuc.ShowTextInRow("Hello World");
Related
I'm binding the ICommand nammed GoToSheet to a Button with ControlTemplate.
<Button Margin="10 10 10 10" Command="{Binding GoToSheet}">
<Button.Template>
<ControlTemplate>
<DockPanel Width="840">
<Border DockPanel.Dock="Left" BorderBrush="Black" BorderThickness="5"></Border>
<Border DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="2"></Border>
<Border DockPanel.Dock="Right" BorderBrush="Black" BorderThickness="2"></Border>
<Border DockPanel.Dock="Bottom" BorderBrush="Black" BorderThickness="2"></Border>
<DockPanel DockPanel.Dock="Left">
<StackPanel Orientation="Vertical" Margin="20 10 0 10">
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical" Margin="0 0 0 10">
<TextBlock Text="{Binding Client}" FontSize="30" FontWeight="Bold" MaxWidth="480"></TextBlock>
<TextBlock Text="{Binding Lieu}" FontSize="22" MaxWidth="480"></TextBlock>
</StackPanel>
<Image Height="35" Width="45" Margin="7 3 0 0" VerticalAlignment="Top" Source="/Resource/Image/CHC.png"></Image>
</StackPanel>
<StackPanel Orientation="Vertical" VerticalAlignment="Bottom" Margin="20 0 0 0">
<TextBlock Text="{Binding MarqueEngin}" FontSize="26" FontWeight="Bold"></TextBlock>
<TextBlock Text="{Binding ModeleEngin}" FontSize="19" FontWeight="Bold"></TextBlock>
</StackPanel>
</StackPanel>
</DockPanel>
<DockPanel DockPanel.Dock="Right" Margin="0 10 20 10">
<TextBlock DockPanel.Dock="Top" Text="{Binding DateIntervention, StringFormat=dd/MM/yyyy}" FontSize="30" HorizontalAlignment="Right"></TextBlock>
<TextBlock DockPanel.Dock="Bottom" Text="{Binding NumeroEngin}" VerticalAlignment="Bottom" FontSize="30" FontWeight="Bold" HorizontalAlignment="Right"></TextBlock>
</DockPanel>
</DockPanel>
</ControlTemplate>
</Button.Template>
</Button>
Why the command only works on the successes areas (see image) ?
Is there a solution for the command works on the entire button area ?
it is caused by lack of background ( button background is NULL).
Please set background for some color, for example:
<Button Margin="10 10 10 10" Background="Transparent" Command="{Binding GoToSheet}">
Here is the solution for similar problem:
Mouse event on transparent background
It should help.
The transparent background must be on the DockPanel in place of Button.
<Button Margin="10 10 10 10" Command="{Binding GoToSheet}">
<Button.Template>
<ControlTemplate>
<DockPanel Width="840" Background="Transparent">
Thanks to macieqqq.
I am trying to create a WPF app that allows you to create rectangles on the screen, resize the rectangles and move them around, and add new re-sizable and movable rectangles on the click of a button.
I don't know much about WPF, so I have found some code that does resizing of shapes here
This resizing works will, but I am now stuck with how to dynamically create a new version of the control. I am using a ContentControl which handles the resizing, and it has an inner Rectangle for display. The xaml looks like the following:
<Window x:Class="MazeBuilder.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:MazeBuilder"
Title="MainWindow" Height="480" Width="640">
<Window.Resources>
<!-- ResizeDecorator Template -->
<ControlTemplate x:Key="ResizeDecoratorTemplate" TargetType="{x:Type Control}">
<Grid>
<s:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 -4 0 0"
VerticalAlignment="Top" HorizontalAlignment="Stretch"/>
<s:ResizeThumb Width="3" Cursor="SizeWE" Margin="-4 0 0 0"
VerticalAlignment="Stretch" HorizontalAlignment="Left"/>
<s:ResizeThumb Width="3" Cursor="SizeWE" Margin="0 0 -4 0"
VerticalAlignment="Stretch" HorizontalAlignment="Right"/>
<s:ResizeThumb Height="3" Cursor="SizeNS" Margin="0 0 0 -4"
VerticalAlignment="Bottom" HorizontalAlignment="Stretch"/>
<s:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="-6 -6 0 0"
VerticalAlignment="Top" HorizontalAlignment="Left"/>
<s:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="0 -6 -6 0"
VerticalAlignment="Top" HorizontalAlignment="Right"/>
<s:ResizeThumb Width="7" Height="7" Cursor="SizeNESW" Margin="-6 0 0 -6"
VerticalAlignment="Bottom" HorizontalAlignment="Left"/>
<s:ResizeThumb Width="7" Height="7" Cursor="SizeNWSE" Margin="0 0 -6 -6"
VerticalAlignment="Bottom" HorizontalAlignment="Right"/>
</Grid>
</ControlTemplate>
<!-- Designer Item Template-->
<ControlTemplate x:Key="DesignerItemTemplate" TargetType="ContentControl">
<Grid DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}}">
<Control Template="{StaticResource ResizeDecoratorTemplate}"/>
<ContentPresenter Content="{TemplateBinding ContentControl.Content}"/>
</Grid>
</ControlTemplate>
</Window.Resources>
<Canvas x:Name="LayoutRoot" MouseDown="LayoutRoot_MouseDown" MouseMove="LayoutRoot_MouseMove">
<Popup Name="PopupEsales" Placement="Right" IsEnabled="True" IsOpen="False" Grid.RowSpan="2">
<ListView Height="145" HorizontalAlignment="Stretch" Margin="0,0,0,0" Name="lvSalesPersonIdSearch" VerticalAlignment="Top" Width="257" >
<ListView.View>
<GridView>
<GridViewColumn Header="Sales Persons Id" Width="100" DisplayMemberBinding="{Binding Path=SPID}" />
<GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Path=Name}" />
</GridView>
</ListView.View>
</ListView>
</Popup>
<Menu Height="23" IsMainMenu="True" HorizontalAlignment="Left" Name="menu1" VerticalAlignment="Top" Width="640">
<MenuItem Header="File">
<MenuItem Header="New Maze" Click="New_Click" />
<MenuItem Header="Load Maze" Click="Load_Click" />
<MenuItem Header="Save Maze" Click="Save_Click" />
</MenuItem>
<MenuItem Header="Tools">
<MenuItem Header="Show" Click="ShowTools_Click" />
</MenuItem>
</Menu>
<ContentControl Width="130"
MinWidth="50"
Height="130"
MinHeight="50"
Canvas.Top="150"
Canvas.Left="470"
Template="{StaticResource DesignerItemTemplate}">
<Rectangle Fill="Blue"
IsHitTestVisible="False"/>
</ContentControl>
<Canvas.Background>
<SolidColorBrush Color="White" Opacity="0"/>
</Canvas.Background>
</Canvas>
The control that I am trying to duplicate dynamically is this bit:
<ContentControl Width="130"
MinWidth="50"
Height="130"
MinHeight="50"
Canvas.Top="150"
Canvas.Left="470"
Template="{StaticResource DesignerItemTemplate}">
<Rectangle Fill="Blue"
IsHitTestVisible="False"/>
</ContentControl>
It is the Template property that is giving me the problem - how do I create it dynamically.
What I have tried is to read in the Xaml into A XamlReader to create the control, like so:
private void CopyBlockWithXaml()
{
StringBuilder sb = new StringBuilder();
sb.Append( #"<ContentControl xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'");
sb.Append( " Width=\"130\" MinWidth=\"50\" Height=\"130\"");
sb.Append(" MinHeight=\"50\" ");
sb.Append(" Canvas.Top=\"150\"");
sb.Append(" Canvas.Left=\"470\"");
sb.Append(" Template=\"{StaticResource DesignerItemTemplate}\">");
sb.Append(" <Rectangle Fill=\"Blue\"");
sb.Append(" IsHitTestVisible=\"False\"/>");
sb.Append("</ContentControl>");
ContentControl cc= (ContentControl) XamlReader.Parse(sb.ToString());
LayoutRoot.Children.Add(cc);
}
Exception:
$exception{"'Provide value on 'System.Windows.StaticResourceExtension' threw an exception.' Line number '1' and line position '258'."} System.Exception {System.Windows.Markup.XamlParseException}
Dynamically creating controls by parsing Xaml at run time will work, but it is probably not the easiest approach.
Instead you could create new instances of the content control using regular C# code. Try something like this in your code behind:
var contentControl = new ContentControl
{
Width = 130,
MinWidth = 50,
Height = 130,
MinHeight = 40
};
Canvas.SetLeft(contentControl, 150);
Canvas.SetTop(contentControl, 470);
contentControl.Content = new Rectangle { Fill = new SolidColorBrush(Color.Blue) };
LayoutRoot.Children.Add(contentControl);
Now this code sample probably won't compile as it is (I don't have a compiler here), but you get the idea. Just create the controls in code as you would do any other classes.
This was the code I ended up using (thanks Rune Grimstad) - it was setting the Template property that was the tricky bit for me to figure out
ContentControl cc = new ContentControl();
ControlTemplate ct = new ControlTemplate();
object rs = this.Resources["DesignerItemTemplate"];
ct = (ControlTemplate)rs;
cc.Template = ct;
cc.Height = 10;
cc.Width = 10;
cc.Content = new Rectangle { Fill = new SolidColorBrush(Color.FromRgb(0,0,255)) };
LayoutRoot.Children.Add(cc);
Canvas.SetLeft(cc, 300);
Canvas.SetTop(cc, 300);
I'm trying to style the back of a WPF chart with some rectangles. I'm using MVVM, and I need the rectangles to be uniformly sized. When defined via Xaml, this works with a fixed "BucketCount" of 4:
<VisualBrush>
<VisualBrush.Visual>
<UniformGrid Height="500" Width="500" Rows="1" Columns="{Binding BucketCount}">
<Rectangle Grid.Row="0" Grid.Column="0" Fill="#22ADD8E6" />
<Rectangle Grid.Row="0" Grid.Column="1" Fill="#22D3D3D3"/>
<Rectangle Grid.Row="0" Grid.Column="2" Fill="#22ADD8E6"/>
<Rectangle Grid.Row="0" Grid.Column="3" Fill="#22D3D3D3"/>
</UniformGrid>
</VisualBrush.Visual>
<VisualBrush>
How can I bind my ObservableCollection of Rectangles? There is no "ItemsSource" property on UniformGrid. Do I need to use an ItemsControl? If so, how can I do this?
Thanks in advance.
You could use ItemsControl to Bind like this. Simple example where ItemsSource is just an ObservableCollection<Brush>
<VisualBrush>
<VisualBrush.Visual>
<ItemsControl x:Name="itemsControl" ItemsSource="{Binding MyBrushes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Height="500" Width="500" Rows="1"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Fill="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</VisualBrush.Visual>
</VisualBrush>
Update
It works for my usage scenario, but I might be missing something here. Here's the full code I've tried. I get the same result from both
MainWindow.xaml
<Grid>
<Grid.Background>
<VisualBrush>
<VisualBrush.Visual>
<ItemsControl x:Name="itemsControl" ItemsSource="{Binding MyBrushes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Height="500" Width="500" Rows="1"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Fill="{Binding}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!--<UniformGrid Height="500" Width="500" Rows="1" Columns="4">
<Rectangle Grid.Row="0" Grid.Column="0" Fill="#22ADD8E6" />
<Rectangle Grid.Row="0" Grid.Column="1" Fill="#22D3D3D3"/>
<Rectangle Grid.Row="0" Grid.Column="2" Fill="#22ADD8E6"/>
<Rectangle Grid.Row="0" Grid.Column="3" Fill="#22D3D3D3"/>
</UniformGrid>-->
</VisualBrush.Visual>
</VisualBrush>
</Grid.Background>
</Grid>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
BrushConverter brushConverter = new BrushConverter();
MyBrushes = new ObservableCollection<Brush>();
MyBrushes.Add(brushConverter.ConvertFrom("#22ADD8E6") as Brush);
MyBrushes.Add(brushConverter.ConvertFrom("#22D3D3D3") as Brush);
MyBrushes.Add(brushConverter.ConvertFrom("#22ADD8E6") as Brush);
MyBrushes.Add(brushConverter.ConvertFrom("#22D3D3D3") as Brush);
this.DataContext = this;
}
public ObservableCollection<Brush> MyBrushes
{
get;
set;
}
}
What do I have to do to get the inner TextBlock below to wrap its text without defining an absolute width?
I've tried Width=Auto, Stretch, TextWrapping, putting it in a StackPanel, nothing seems to work.
(source: deviantsart.com)
XAML:
<UserControl x:Class="Test5.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tk="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit"
Width="800"
Height="600">
<tk:DockPanel LastChildFill="True">
<StackPanel tk:DockPanel.Dock="Top"
Width="Auto"
Height="50"
Background="#eee">
<TextBlock Text="{Binding TopContent}"/>
</StackPanel>
<StackPanel tk:DockPanel.Dock="Bottom" Background="#bbb"
Width="Auto"
Height="50">
<TextBlock Text="bottom area"/>
</StackPanel>
<StackPanel tk:DockPanel.Dock="Right" Background="#ccc"
Width="200"
Height="Auto">
<TextBlock Text="info panel"/>
</StackPanel>
<StackPanel tk:DockPanel.Dock="Left" Background="#ddd"
Width="Auto"
Height="Auto">
<ScrollViewer HorizontalScrollBarVisibility="Auto" Padding="10"
BorderThickness="0"
Width="Auto"
VerticalScrollBarVisibility="Auto">
<tk:DockPanel HorizontalAlignment="Left" Width="Auto" >
<StackPanel tk:DockPanel.Dock="Top" HorizontalAlignment="Left">
<Button Content="Add More" Click="Button_Click"/>
</StackPanel>
<TextBlock tk:DockPanel.Dock="Top"
Text="{Binding MainContent}"
Width="Auto"
TextWrapping="Wrap" />
</tk:DockPanel>
</ScrollViewer>
</StackPanel>
</tk:DockPanel>
</UserControl>
Have you tried changing the scrollbar visibility?
<ScrollViewer HorizontalScrollBarVisibility="Disabled" Padding="10"
BorderThickness="0"
Width="Auto"
VerticalScrollBarVisibility="Auto">
For some reason the obvious (elegant) solution doesn't seem to work in SL:
<ScrollViewer Name="w_scrollViewer" HorizontalScrollBarVisibility="Auto" Padding="10"
BorderThickness="0" Width="Auto" VerticalScrollBarVisibility="Auto">
<tk:DockPanel Name="w_dp" HorizontalAlignment="Left" Width="Auto" >
<TextBlock Margin="2" Name="w_tb"
Text="{Binding MainContent}" TextWrapping="Wrap"
Width="{Binding ActualWidth, ElementName=w_scrollViewer}"
/>
</tk:DockPanel>
</ScrollViewer>
but a bit more "rough" way:
<StackPanel SizeChanged="w_sp_SizeChanged" Name="w_sp" tk:DockPanel.Dock="Left"
Background="#ddd" Width="Auto" Height="Auto">
<ScrollViewer Name="w_scrollViewer" HorizontalScrollBarVisibility="Auto"
Padding="10" Width="Auto" VerticalScrollBarVisibility="Auto"
SizeChanged="HandleScrollViewerSizeChanged" >
<tk:DockPanel HorizontalAlignment="Left" Width="Auto" >
<TextBlock Name="w_textBlock" Text="{Binding MainContent}" TextWrapping="Wrap" />
</tk:DockPanel>
</ScrollViewer>
</StackPanel>
code behind:
private void HandleScrollViewerSizeChanged(object sender, SizeChangedEventArgs e)
{
w_textBlock.Width = w_scrollViewer.ActualWidth;
}
Here's my XAML code:
<Window x:Class="CarFinder.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Search for cars in TuMomo" Height="480" Width="600">
<DockPanel Margin="8">
<Border CornerRadius="6"
BorderBrush="Gray"
Background="LightGray"
BorderThickness="2"
Padding="8">
<StackPanel Orientation="Horizontal"
DockPanel.Dock="Top"
Height="25">
<TextBlock FontSize="14" Padding="0 0 8 0">
Search:
</TextBlock>
<TextBox x:Name="txtSearchTerm" Width="400" />
<Image Source="/CarFinder;component/Images/Chrysanthemum.jpg" />
</StackPanel>
</Border>
<StackPanel Orientation="Horizontal"
DockPanel.Dock="Top"
Height="25">
</StackPanel>
</DockPanel>
</Window>
The border is set around the entire window. And also, when I create another StackPanel it's added to the right of my previous StackPanel instead of being added under it. What's the reason for this?
What about this one :
<DockPanel Margin="8">
<Border CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="2" DockPanel.Dock="Top">
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="14" Padding="0 0 8 0" HorizontalAlignment="Center" VerticalAlignment="Center">Search:</TextBlock>
<TextBox x:Name="txtSearchTerm" HorizontalAlignment="Center" VerticalAlignment="Center" />
<Image Source="lock.png" Width="32" Height="32" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
</Border>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom" Height="25" />
</DockPanel>
You set DockPanel.Dock="Top" to the StackPanel, but the StackPanel is not a child of the DockPanel... the Border is. Your docking property is being ignored.
If you move DockPanel.Dock="Top" to the Border instead, both of your problems will be fixed :)
May be it will helpful:
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left" Height="160" Margin="10,55,0,0" VerticalAlignment="Top" Width="492"/>