Show WPF window with WindowStyle=None and no resize - wpf

I'm trying to display a window with a border like with WindowStyle="None" (picture 2), but without the option to resize it!
When I set the ResizeMode="NoResize" the border disappears (picture 1)
Does anyone know how to do this?

You can get around this by setting the Max and Min size properties to the same values, thus forcing the Window to have a fixed size. Something like this should work:
Window w = new Window();
w.MaxHeight = w.MinHeight = 300;
w.MinWidth = w.MaxWidth = 400;
w.WindowStyle = System.Windows.WindowStyle.None;
w.Show();

Related

Display Image in Modal Window

I am trying to display am image with two methods: First is when user has full absolute path and if they don't have it and if image is stored in sqlite database as blob object.
Here is my code:
var modal = new Modal();
modal.HorizontalAlignment = HorizontalAlignment.Center;
modal.VerticalAlignment = VerticalAlignment.Center;
modal.Title = "View Image";
modal.ResizeMode = ResizeMode.CanResizeWithGrip;
Grid grid = new Grid();
RowDefinition row1 = new RowDefinition() {Height = GridLength.Auto};
grid.RowDefinitions.Add(row1);
if (!string.IsNullOrEmpty(ImageFileLocation))
{
Image i = new Image();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri(ImageFileLocation, UriKind.Absolute);
bi.EndInit();
i.Source = bi;
//i.Height = 400;
//i.Width = 400;
i.MinHeight = 400;
i.MinWidth = 400;
i.Stretch = Stretch.Fill;
i.HorizontalAlignment = HorizontalAlignment.Stretch;
i.VerticalAlignment = VerticalAlignment.Stretch;
grid.Children.Add(i);
modal.ModalContentControl.Content = grid;
}
else if (ImageStore.ImageStoreId != null)
{
//TODO: Create Image object to display
MessageBox.Show(ImageStore.ImageStoreId.ToString());
}
modal.ShowDialog();
Here is my Modal.. It is basically a window that I can re-use as a modal dialog box.
<Window x:Class="Screens.Modal"
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:Screens"
mc:Ignorable="d"
Title="Modal" ResizeMode="NoResize"
SizeToContent="WidthAndHeight" x:Name="ModalWindow" WindowStartupLocation="CenterScreen">
<Grid>
<ContentControl x:Name="ModalContentControl" />
</Grid>
</Window>
Everything in code above works except one thing. when Modal is displayed with an image, modal comes out to be super huge and it fills almost my two monitors. I want to achieve two things with this:
I want modal to be resizable and so does the image inside it (responsive approach)
I want image to be proportionately growing since most of the image I use will be rectangle image and not square.
This might be easier and I probably can Google it and find out but since I am asking related question here.. How can I use the BLOB to create an Image object?
Please remove extra parameters I have with modal and/or image object if you think it is not needed to achieve what I want.
Avoid setting the SizeToContent property of the modal window to WidthAndHeight in your XAML markup. Keep its default value of Manual.

What is wrong with Canvas.SetTop(element,position)

I am trying to move and reposition elements with mouse and through code. But I guess I'm missing something or do something the wrong way. So I built a little sample app. It's just a empty wpf app with this MainWindow function
public MainWindow()
{
InitializeComponent();
Label lText = new Label();
lText.Content = "this is my test label";
lText.Height = 50;
lText.Width = 50;
lText.Background = Brushes.Aqua;
// do I really need to do this?
lText.VerticalAlignment = System.Windows.VerticalAlignment.Top;
lText.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
// this part already fails
Canvas.SetTop(lText, 20);
Canvas.SetLeft(lText, 10);
this.Content = lText;
}
The attached properties Canvas.Left and Canvas.Top (which in code behind are set by the Canvas.SetLeft and Canvas.SetTop methods) only have an effect for an element that is a direct child of a Canvas control.
So declare a Canvas as Content element in your MainWindow's XAML:
<Window ...>
<Canvas x:Name="canvas" />
</Window>
Then add the element to the Canvas' Children collection in code behind:
Canvas.SetTop(lText, 20);
Canvas.SetLeft(lText, 10);
canvas.Children.Add(lText);
The solution was that the Left and Top properties must be set inside the Xaml before you can read it. I was always getting NaN and therefor couldn't set the correct value either.

take a screenshot of WPF datagrid with scrollviewer

I'm trying to take a screenshot of a datagrid which has to many rows to be displayed. So there is a scrollviewer.
So when I just put the datagrid into the Render Method of RenderTargetBitmap I obviously just get the viewable part of the datagrid.
I read that one can take a screenshot of a content when actually rendering the ItemsPresenter of the ScrollViewer of that control, as the ItemsPresenter would have the "real" Width and Height of the content.
Unfortunatly my ScrollViewer doesnt have any different Height, ActualHeight or RenderSize.Height than the dataGrid.
So I always just get the visible part of the Content.
Anyone know how to do this the right way, that it actually takes the whole content ?
Code:
var scroll = GetTemplateChildByName(dataGridInOut);
if (scroll != null)
{
var item = scroll.Content as ItemsPresenter;
var width = item.RenderSize.Width;
var height = item.RenderSize.Height;
var rtb = new RenderTargetBitmap((int) Math.Round(width), (int)Math.Round(height), 96, 96,
PixelFormats.Pbgra32);
var drawingVisual = new DrawingVisual();
var visualBrush = new VisualBrush(item);
using (var context = drawingVisual.RenderOpen())
{
context.DrawRectangle(visualBrush, null, new Rect(new Point(0,0), new Size(width, height)));
}
rtb.Render(drawingVisual);
Clipboard.SetImage(rtb);
}
Leaf is right. You could instantiate another DataGrid bound to the same source programmatically, put it into a container which gives it infinite space, wait for it to render and then take a screenshot of this. No need to actually show it in the UI.

How to remove the border of a Button in code?

I am developing a windows phone 7 application. I am new to windows phone 7 applications. In my application I have created a button control dynamically and added the background image to the button control as follows.
Button AlphabetButton = new Button();
AlphabetButton.Content = vAlphabet;
ImageBrush brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("button_off.png", UriKind.Relative));
//brush.Stretch = Stretch.None;
AlphabetButton.Background = brush;
AlphabetButton.BorderBrush = new SolidColorBrush(Colors.Gray);
AlphabetButton.Margin = new Thickness(-12, -27, 0, 0);
AlphabetButton.Width = 80;
AlphabetButton.Height = 80;
I want to remove the border of the button control because with that border the image does not appear as per requirement. How to do this? Can we do this with the BorderThickness attribute of button control or is there any other way? Can you please provide me any code or link through which I can resolve the above issue? If I am doing anything wrong then please guide me.
The easiest way is to set the BorderThickness to 0 like for Example:
Button alphabetButton = new Button();
alphabetButton.BorderThickness = new Thickness(0.0);
Another option could be to set the BorderBrush to Transparent or to change the whole Style of the button and especially its ControlTemplate.
I think Button.StrokeTHickness is the correct property to adjust the borders.

Silverlight: Auto Scrolling down with Scrollviewer

I have a scrollviewer with dynamic content. On an event, a new content is made visible and the scrollbar appears. How do i make it auto scroll to have that content in view?
Thanks,
Shawn Mclean
Use ScrollToVerticalOffset() to do this, passing in the coordinates of the new content.
var newContent = GetNewContent();
var generalTransform = newContent.TransformToVisual(
Application.Current.RootVisual as UIElement);
Point offset = generalTransform.Transform(new Point(0, 0));
double controlTop = offset.Y;
double controlLeft = offset.X;
scrollViewer.ScrollToVerticalOffSet(controlTop);
scrollViewer.ScrollToHorizontalOffSet(controlLeft);
Are you sure Scrollviewer is the control you need here?
Sounds to me like you ought to be using ListBox (which you can heavily style if necessary). It has a ScrollIntoView(item) method which would acheive your goal.

Resources