MessageBox in material design wpf c# - c#

I’m using Material Design In XAML Toolkit C# ,MessageBox in material design WPF C# ,
I need like this or better Design for MessageBox 👌
I’ve tried use DialogHost but this Error happens
No loaded DialogHost instances.
private async void MenuPopupButton_OnClick(object sender, RoutedEventArgs e) {
var sampleMessageDialog = new SampleMessageDialog {
Message = {Text = "Goodbye"}
};
await DialogHost.Show(sampleMessageDialog, "RootDialog");
}
No loaded DialogHost instances.

I have developed one custom message box(using material design controls) in my application.
i'll show you the code below of WPF XAML and C#.
it contains three types of message boxes as below.
Yes,No
Ok
Ok,Cancel
Download code from below link.
https://github.com/sandeepjadhav75502/CustomMessageBoxWPF
WPF XAML Code
Heading
<Window x:Class="EVotingDashBoard.MessageBoxCustom"
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"
mc:Ignorable="d"
Title="MessageBoxWindow" Height="220" Width="500"
WindowStartupLocation="CenterScreen" WindowStyle="None" Background="White"
ResizeMode="CanResize" Topmost="True" ShowInTaskbar="False"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
TextElement.Foreground="{DynamicResource MaterialDesignBody}"
TextElement.FontWeight="Regular"
TextElement.FontSize="10"
TextOptions.TextFormattingMode="Ideal"
TextOptions.TextRenderingMode="Auto"
FontFamily="{DynamicResource MaterialDesignFont}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="197*"/>
<ColumnDefinition Width="295*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<materialDesign:Card x:Name="cardHeader" Grid.Row="0" Padding="10" Margin="0" materialDesign:ShadowAssist.ShadowDepth="Depth5" Background="{DynamicResource PrimaryHueDarkBrush}" Foreground="{DynamicResource PrimaryHueDarkForegroundBrush}" Visibility="Visible" Grid.ColumnSpan="2">
<StackPanel>
<TextBlock x:Name="txtTitle" HorizontalAlignment="Center" VerticalAlignment="Stretch" Style="{DynamicResource MaterialDesignTitleTextBlock}" FontSize="20" >Message Title</TextBlock>
</StackPanel>
</materialDesign:Card>
<StackPanel HorizontalAlignment="Right" Margin="0,5,5,0" VerticalAlignment="Top" Grid.Column="1">
<Button x:Name="btnClose" Click="btnClose_Click" Width="35" Height="35" Background="White" Foreground="{DynamicResource PrimaryHueDarkBrush}" Style="{StaticResource MaterialDesignFloatingActionDarkButton}">
<materialDesign:PackIcon Kind="Close" />
</Button>
</StackPanel>
<Grid Grid.Row="1" Grid.ColumnSpan="2">
<Grid Margin="20">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<materialDesign:TransitioningContent>
<materialDesign:TransitioningContent.OpeningEffects >
<materialDesign:TransitionEffect Kind="FadeIn" />
<materialDesign:TransitionEffect Kind="SlideInFromBottom" />
</materialDesign:TransitioningContent.OpeningEffects>
<TextBox x:Name="txtMessage" HorizontalAlignment="Center" IsReadOnly="True" Grid.Row="0" Margin="5" materialDesign:HintAssist.Hint="" FontSize="18" Style="{StaticResource MaterialDesignFloatingHintTextBox}" />
</materialDesign:TransitioningContent>
</Grid>
<Grid Grid.Row="1" Margin="0,20,0,5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="btnOk" Click="btnOk_Click" Grid.Column="1" Style="{StaticResource MaterialDesignRaisedButton}" Margin="5" Width="100" Content="OK" ToolTip="Ok"/>
<Button x:Name="btnCancel" Click="btnCancel_Click" Grid.Column="2" Style="{StaticResource MaterialDesignRaisedButton}" Margin="5" Width="100" Content="Cancel" ToolTip="Cancel"/>
<Button x:Name="btnYes" Click="btnYes_Click" Grid.Column="1" Style="{StaticResource MaterialDesignRaisedButton}" Margin="5" Width="100" Content="Yes" ToolTip="Yes"/>
<Button x:Name="btnNo" Click="btnNo_Click" Grid.Column="2" Style="{StaticResource MaterialDesignRaisedButton}" Margin="5" Width="100" Content="No" ToolTip="No"/>
</Grid>
</Grid>
</Grid>
</Grid>
</Window>
C# Code Behind
/// <summary>
/// Interaction logic for MessageBoxCustom.xaml
/// </summary>
public partial class MessageBoxCustom : Window
{
public MessageBoxCustom(string Message, MessageType Type, MessageButtons Buttons)
{
InitializeComponent();
txtMessage.Text = Message;
switch (Type)
{
case MessageType.Info:
txtTitle.Text = "Info";
break;
case MessageType.Confirmation:
txtTitle.Text = "Confirmation";
break;
case MessageType.Success:
{
txtTitle.Text = "Success";
}
break;
case MessageType.Warning:
txtTitle.Text = "Warning";
break;
case MessageType.Error:
{
txtTitle.Text = "Error";
}
break;
}
switch (Buttons)
{
case MessageButtons.OkCancel:
btnYes.Visibility = Visibility.Collapsed; btnNo.Visibility = Visibility.Collapsed;
break;
case MessageButtons.YesNo:
btnOk.Visibility = Visibility.Collapsed; btnCancel.Visibility = Visibility.Collapsed;
break;
case MessageButtons.Ok:
btnOk.Visibility = Visibility.Visible;
btnCancel.Visibility = Visibility.Collapsed;
btnYes.Visibility = Visibility.Collapsed; btnNo.Visibility = Visibility.Collapsed;
break;
}
}
private void btnYes_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
this.Close();
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
this.Close();
}
private void btnOk_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
this.Close();
}
private void btnNo_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
this.Close();
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
this.Close();
}
}
public enum MessageType
{
Info,
Confirmation,
Success,
Warning,
Error,
}
public enum MessageButtons
{
OkCancel,
YesNo,
Ok,
}
we can call it like
private void Button_Close(object sender, RoutedEventArgs e)
{
bool? Result = new MessageBoxCustom("Are you sure, You want to close
application ?", MessageType.Confirmation, MessageButtons.YesNo).ShowDialog();
if (Result.Value)
{
Application.Current.Shutdown();
}
}

Consider using this package that has the following extra benefit features:
Custom styles for border window, message foreground and background, title foreground and background, border, etc
Button to copy message box details to clipboard
Scrollable message box content
Message content is .NET UIElement which can host any content

Related

My WPF ChromeWindow is slightly off when maximized

So I built this sample application and using a custom WindowChrome with WPF and C#. Everytime I try to maximize my window using the buttons I made, the maximized window becomes slightly off? Like by a few pixels. What is happening?
<Window x:Class="TestApp.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:TestApp"
mc:Ignorable="d"
Title="TestApp"
WindowStyle="None"
WindowStartupLocation="CenterScreen"
MinHeight="500" MinWidth="940">
<WindowChrome.WindowChrome>
<WindowChrome
ResizeBorderThickness="6"
CaptionHeight="20"
GlassFrameThickness="0"
CornerRadius="0"
/>
</WindowChrome.WindowChrome>
<!-- Application -->
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="26" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- WindowChrone Header -->
<Grid Background="{StaticResource WindowChromeBrush}" Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<Button Grid.Column="0" Content="RelayC" />
<Button Click="MinimizeWindow" Grid.Column="2" Style="{StaticResource WindowChromeButton}">
<Image Source="{StaticResource window_minimize_icon}"/>
</Button>
<Button Click="MaximizeWindow" Grid.Column="3" Style="{StaticResource WindowChromeButton}">
<Image Source="{StaticResource window_maximize_icon}" />
</Button>
<Button Click="CloseWindow" Grid.Column="4" Style="{StaticResource WindowChromeButton}">
<Image Source="{StaticResource window_close_icon}" />
</Button>
</Grid>
<!--- Main Content -->
<Grid Grid.Row="1">
</Grid>
</Grid>
</Window>
Here is my sample code for the Button Functions:
private void MinimizeWindow(object sender, RoutedEventArgs e) => this.WindowState = WindowState.Minimized;
private void MaximizeWindow(object sender, RoutedEventArgs e) => this.WindowState = WindowState.Maximized;
private void CloseWindow(object sender, RoutedEventArgs e) => this.Close();
What am I doing wrong?
I ended up finding a sort of elegant solution to this problem. I simply assigned a name to the outermost grid, detected whenever it was maximized and added a margin to make sure the application was fully encompassing the screen and not going over. Then, when the screen size changed, I would remove said margin.
Here is some of my code:
Main functions:
private bool isMaximized = false;
private void CheckMaximizeChange(object sender, SizeChangedEventArgs e)
{
if (WindowState == WindowState.Maximized || (isMaximized && WindowState != WindowState.Minimized)) ChangeWindow();
}
private void MaximizeWindow(object sender, RoutedEventArgs e) => this.WindowState ^= WindowState.Maximized;
private void ChangeWindow()
{
isMaximized = !isMaximized;
MaximizeImageButton.Source = FindResource(MaximizeImageButton.Source == FindResource("window_maximize_icon") ? "window_restore_icon" : "window_maximize_icon") as DrawingImage;
MainGrid.Margin = new Thickness(MainGrid.Margin == new Thickness(0) ? 7 : 0);
}
then in the constructor of the application:
SizeChanged += CheckMaximizeChange;

wpf- making an entire box clickable

I have a rectangle textbox in WPF and there is already a button inside the textbox which is clickable. I need to make the entire rectangle textbox clickable, so users can click anywhere in the textbox rather than just the button to interact.
Is it possible to do so?
Here is my XAML:
<Grid Background="{StaticResource TinyGrayBrush}" Height="260" Width="530" Margin="10,10,10,10" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="120*"/>
<ColumnDefinition Width="140*"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Image x:Name="imgCard" Source="{StaticResource Exception-NoImageAvailable}" Stretch="Fill" />
<Image x:Name="hotpickEn" Source="{StaticResource HotPickEn}" Height="20" Width="70" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0" Visibility="Hidden" />
<Image x:Name="hotpickCh" Source="{StaticResource HotPickCH}" Height="20" Width="70" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,30,0,0" Visibility="Hidden" />
</Grid>
<Grid x:Name="gdEnglish" Visibility="Visible" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="12*"/>
<RowDefinition Height="62*"/>
<RowDefinition Height="90*"/>
<RowDefinition Height="5*"/>
<RowDefinition Height="55*"/>
<RowDefinition Height="10*"/>
</Grid.RowDefinitions>
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="25,5,25,5"/>
</Style>
</Grid.Resources>
<TextBlock Grid.Row="1" x:Name="txtTitle" Text="SPARKLING SUNDAY BRUNCH BUFFET" FontSize="20" TextWrapping="Wrap" FontWeight="Medium" Foreground="{StaticResource SemiDarkGrayBrush}" Margin="5,0,5,6.5" />
<TextBlock Grid.Row="2" x:Name="txtDesc" Text="Pop your Sunday Morning with luxury sparkling bubble" FontSize="16" TextWrapping="Wrap" Foreground="{StaticResource GrayBrush}" Margin="5,3.5,5,6.5" />
<TextBlock Grid.Row="3" x:Name="txtPoint" Text="138,900 GP" FontSize="21" TextWrapping="Wrap" FontWeight="Medium" Foreground="{StaticResource GrayBrush}" Margin="25,3.5,25,6" />
<Button Grid.Row="4" x:Name="btnFindOutMore" Content="FIND OUT MORE OR REDEEM" Style="{StaticResource SmallWhiteRedButton}" Margin="12,0,12,0" FontSize="20" />
</Grid>
<Grid x:Name="gdChinese" Visibility="Hidden" Grid.Column="1">
<Grid.RowDefinitions>
<RowDefinition Height="12*"/>
<RowDefinition Height="62*"/>
<RowDefinition Height="90*"/>
<RowDefinition Height="5*"/>
<RowDefinition Height="55*"/>
<RowDefinition Height="10*"/>
</Grid.RowDefinitions>
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="25,5,25,5"/>
</Style>
</Grid.Resources>
<TextBlock Grid.Row="1" x:Name="txtCHTitle" Text="SPARKLING SUNDAY BRUNCH BUFFET" FontSize="20" TextWrapping="Wrap" FontWeight="Medium" Foreground="{StaticResource SemiDarkGrayBrush}" HorizontalAlignment="Left" Margin="5,4.667,0,5.333" />
<TextBlock Grid.Row="2" x:Name="txtCHDesc" Text="Pop your Sunday Morning with luxury sparkling bubble" FontSize="16" TextWrapping="Wrap" Foreground="{StaticResource GrayBrush}" HorizontalAlignment="Left" Margin="5,4.667,0,5.667" />
<TextBlock Grid.Row="3" x:Name="txtCHPoint" Text="138,900 GP" FontSize="21" TextWrapping="Wrap" FontWeight="Medium" Foreground="{StaticResource GrayBrush}" HorizontalAlignment="Left" Margin="25,3.5,25,6" Width="105" />
<Button x:Name="btnCHFindOutMore" Grid.Row="4" Content="FIND OUT MORE OR REDEEM" Style="{StaticResource SmallWhiteRedButton}" Margin="15,0,10,0"/>
</Grid>
As you are using Code-Behind for this:
You basically subscribe to the events of the Grid too
gdEnglish.TouchDown += gdEnglish_TouchDown;
gdEnglish.TouchUp += gdEnglish_TouchUp;
gdEnglish.TouchLeave += gdEnglish_TouchLeave;
gdEnglish.MouseDown += gdEnglish_MouseDown;
gdEnglish.MouseUp += gdEnglish_MouseUp;
gdEnglish.MouseLeave += gdEnglish_MouseLeave;
add a new private variable to indicate the current state
private bool gdEnglish_clickflag = false;
and create the different event handlers
private void gdEnglish_MouseLeave(object sender, MouseEventArgs e)
{
gdEnglish_clickflag = false;
}
private void gdEnglish_MouseUp(object sender, MouseButtonEventArgs e)
{
if (gdEnglish_clickflag)
{
gdEnglish_clickflag = false;
e.Handled = true;
//////////
// YOUR //
// CODE //
//////////
}
}
private void gdEnglish_MouseDown(object sender, MouseButtonEventArgs e)
{
gdEnglish_clickflag = true;
}
private void gdEnglish_TouchLeave(object sender, TouchEventArgs e)
{
gdEnglish_clickflag = false;
}
private void gdEnglish_TouchUp(object sender, TouchEventArgs e)
{
if (gdEnglish_clickflag)
{
gdEnglish_clickflag = false;
e.Handled = true;
//////////
// YOUR //
// CODE //
//////////
}
}
private void gdEnglish_TouchDown(object sender, TouchEventArgs e)
{
gdEnglish_clickflag = true;
}
I still want to recommend that you research the topic MVVM with WPF
it will make things like this more clean and easy

C# Combobox calling comboboxitem

I'm working on an Windows Application where you can block and allow programs. I have a problem on how to call the comboboxitem. inc is an item inside combobox 1 and all is an item inside combobox 2. I want to create a condition where if both comboboxitem are selected run this. How do i do that ? Thank you
private void addSubmitBtn_Click(object sender, RoutedEventArgs e)
{
foreach (string path in pathT.Text.Split(';'))
if (File.Exists(path))
{
if ( inc.SelectedIndex > -1 && all.SelectedIndex > -1)
{
FWRule(path, NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_IN, (blo.Checked) ? NET_FW_ACTION_.NET_FW_ACTION_BLOCK : NET_FW_ACTION_.NET_FW_ACTION_ALLOW, ((Control)sender).Tag.ToString());
}
if (outg.Checked || all.Checked)
{
FWRule(path, NET_FW_RULE_DIRECTION_.NET_FW_RULE_DIR_OUT, (blo.Checked) ? NET_FW_ACTION_.NET_FW_ACTION_BLOCK : NET_FW_ACTION_.NET_FW_ACTION_ALLOW, ((Control)sender).Tag.ToString());
}
}
((System.Windows.Controls.Panel)this.Parent).Children.Remove(this);
}
private void FWRule(string path, NET_FW_RULE_DIRECTION_ d, NET_FW_ACTION_ fwaction, string action)
{
try
{
INetFwRule firewallRule = (INetFwRule)Activator.CreateInstance(
Type.GetTypeFromProgID("HNetCfg.FWRule"));
firewallRule.Action = fwaction;
firewallRule.Enabled = true;
firewallRule.InterfaceTypes = "All";
firewallRule.ApplicationName = path;
firewallRule.Name = "PRST: " + System.IO.Path.GetFileName(path);
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallRule.Direction = d;
if (action == "1") firewallPolicy.Rules.Add(firewallRule);
else
{
((System.Windows.Controls.Panel)this.Parent).Children.Remove(this);
}
}
catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message, "ERROR"); }
}
private void addCancelBtn_Click(object sender, RoutedEventArgs e)
{
((System.Windows.Controls.Panel)this.Parent).Children.Remove(this);
}
private void addBackBtn_Click(object sender, RoutedEventArgs e)
{
((System.Windows.Controls.Panel)this.Parent).Children.Remove(this);
}
private void pathT_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void blo_CheckedChanged(object sender, EventArgs e)
{
}
private void al_CheckedChanged(object sender, EventArgs e)
{
}
private void Button_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "Executable|*.exe";
fd.Multiselect = true;
if (fd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
pathT.Text = String.Join(";", fd.FileNames);
}
}
}
}
This is the XAML part
<UserControl x:Class="WpfDeepTest.Views.policyAddFunc"
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:md="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:local="clr-namespace:WpfDeepTest.Views"
mc:Ignorable="d"
d:DesignHeight="700" d:DesignWidth="960">
<Grid Background="{DynamicResource MaterialDesignPaper}">
<Grid.RowDefinitions>
<RowDefinition Height=".5*"/>
<RowDefinition Height="3*"/>
<RowDefinition Height="1*"/>
<RowDefinition Height=".5*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="20 0 0 0">
<Button Name="addBackBtn" Style="{DynamicResource MaterialDesignFloatingActionButton}" Width="40" Height="40" Click="addBackBtn_Click">
<md:PackIcon Kind="ChevronLeft" Height="30" Width="30"/>
</Button>
</Grid>
<md:Card Grid.Row="1" Margin="15 10" Padding="100 0" Height="400">
<StackPanel VerticalAlignment="Center">
<Grid Margin="0 0 0 0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="1.5*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="Application Path: " VerticalAlignment="Center" FontSize="25" Foreground="#616161" Padding="10 0 0 0"/>
<TextBox Name="pathT" Grid.Column="1" Background="#F5F5F5" VerticalContentAlignment="Center" FontSize="20" md:HintAssist.Hint="Ex: InboundPolicy#1" Padding="5 0" Margin="0,0,57.2,10.2" TextChanged="TextBox_TextChanged_1"/>
<Button Content="..." Grid.Column="1" HorizontalAlignment="Left" Margin="372,-1,0,0" VerticalAlignment="Top" Width="47" Height="36" Click="Button_Click"/>
</Grid>
<Grid Margin="0 30 0 0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="1.5*"/>
</Grid.ColumnDefinitions>
</Grid>
<Grid Margin="0 30 0 0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="280*"/>
<ColumnDefinition Width="138*"/>
<ColumnDefinition Width="13*"/>
<ColumnDefinition Width="31*"/>
<ColumnDefinition Width="66*"/>
<ColumnDefinition Width="171*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="Directions: " VerticalAlignment="Center" FontSize="25" Foreground="#616161" Padding="10 0 0 0" Margin="0,-25,0,25"/>
<ComboBox x:Name="Combobox1" Grid.Column="1" Background="#F5F5F5" VerticalContentAlignment="Center" FontSize="20" Padding="5 0" Margin="0,-31,0.2,39" Grid.ColumnSpan="5">
<ComboBoxItem Name="all" Content="All" HorizontalAlignment="Left" Width="417.6"/>
<ComboBoxItem Name="inc" IsSelected="True" >OutBound</ComboBoxItem>
<ComboBoxItem Name="outg">InBound</ComboBoxItem>
</ComboBox>
</Grid>
<Grid Margin="0 30 0 0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="280*"/>
<ColumnDefinition Width="108*"/>
<ColumnDefinition Width="61*"/>
<ColumnDefinition Width="163*"/>
<ColumnDefinition Width="87*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="Action: " VerticalAlignment="Center" FontSize="25" Foreground="#616161" Padding="10 0 0 0" Grid.ColumnSpan="2" Margin="2,-34,106,32.4"/>
<ComboBox x:Name="Combobox2" SelectedItem="" Grid.Column="1" Background="#F5F5F5" VerticalContentAlignment="Center" FontSize="20" Padding="5 0" Margin="2,-30,-1.8,36.4" Grid.ColumnSpan="4">
<ComboBoxItem Name="blo" IsSelected="True">Block</ComboBoxItem>
<ComboBoxItem Name="al">Allow</ComboBoxItem>
</ComboBox>
</Grid>
</StackPanel>
</md:Card>
<StackPanel Orientation="Horizontal" Grid.Row="2" HorizontalAlignment="Center">
<Button Name="addSubmitBtn" Style="{DynamicResource MaterialDesignRaisedButton}" VerticalAlignment="Top" Margin="10 20" Content="Submit" Width="150" Height="35" TabIndex="1" Click="addSubmitBtn_Click"/>
<Button Name="addCancelBtn" Style="{DynamicResource MaterialDesignRaisedLightButton}" VerticalAlignment="Top" Margin="10 20" Content="Cancel" Width="150" Height="35" Click="addCancelBtn_Click"/>
</StackPanel>
</Grid>
Here is a screenshot of the program
enter image description here
UPDATE
combobox items can be selected but not checked right? you should check if they are selected only. unless you build your own control that has a combobox of checkboxe items that can be checked
i think I found your problem, on your if condition, the situation can never happen because inc and all are part of <ComboBox x:Name="Combobox1", and in your code you are saying if ( inc.SelectedIndex > -1 && all.SelectedIndex > -1) those two can not be selected in the same time ;)
This for example should work because it has an OR operator if (outg.Checked || all.Checked), thous only one of those should be selected.
ignore below part, is a solution thinking that you were using Win Forms
you can use the selectedIndexChanged
like this for example:
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
this.getValues(this.comboBox2, this.comboBox3);
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
this.getValues(this.comboBox2, this.comboBox3);
}
public void compareValues(String value1, String value2)
{
if(value1.Equals("inc") && value2.Equals("all"))
{
this.runThis();
}
}
public void runThis()
{
//do your thing here :)
this.label1.Text = "run this!!!";
}
public void getValues(ComboBox cmb2, ComboBox cmb3)
{
String cmb1Value = "";
String cmb2Value = "";
try{
cmb1Value = this.comboBox2.SelectedItem.ToString();
}catch (Exception exception){
//not both set
}
try
{
cmb2Value = this.comboBox3.SelectedItem.ToString();
}catch (Exception exception){
//not both set
}
this.compareValues(cmb1Value, cmb2Value);
}
example of above code, running:

Updating Canvas along with Databound properties

I created a class "MyPolygonClass" from which I created an instance that I have databound to the User Control.
This works fine.
However, if the user changes the number of polygon sides I would like to update a canvas, also shown in the User Contol - but not part of MyPolygonClass
I initially run UpdateCanvas(), but it doesn't update if _poly is updated. How do I call UpdateCanvas() everytime _poly is changed?
public partial class MainWindow : Window
{
private MyPolygonClass _poly;
public MainWindow()
{
InitializeComponent();
}
public void UpdateCanvas()
{
//Create a canvas
Canvas drawingCanvas = GenerateCanvas(this._poly.CornerCount);
//Overwrite the existing Canvas
var vb = drawingPlaceholder.Parent as Viewbox;
if (vb != null)
{
vb.Child = drawingCanvas;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
worker.DoWork += worker_DoWork;
worker.RunWorkerAsync();
}
void worker_DoWork(object sender, DoWorkEventArgs e)
{
//Gets the Info from a Database
this._poly = DataSupplier.GetPolygon();
}
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//Do Databinding Here
Menu.DataContext = this._poly;
// Display
UpdateCanvas();
}
}
XAML:
<Grid.Resources>
<DataTemplate x:Key="LegTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="20"></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
<ColumnDefinition Width="3*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Viewbox Grid.Row="0" Grid.Column="0" >
<Label Name="IdLabel" Content="{Binding Path=Name}"/>
</Viewbox>
<WrapPanel Grid.Row="0" Grid.Column="3">
<Button FontFamily="Marlett" Name="ButtonUp" Content="5" Click="Button_Click_Move_Leg_Up" Style="{StaticResource LegButtonUp}"></Button>
<Button FontFamily="Marlett" Name="ButtonDown" Content="6" Click="Button_Click_Move_Leg_Down" Style="{StaticResource LegButtonDown}"></Button>
<Button Name="ButtonDelete" Content="x" Click="Button_Click_Delete_Leg" Style="{StaticResource LegButtonDel}"></Button>
</WrapPanel>
<TextBox Grid.Row="0" Grid.Column="1" Name="TextBoxLength" Text="{Binding Path=Length, Converter={StaticResource DoubleConverter}, ConverterCulture=de-de, Mode=TwoWay, StringFormat=N1}" TextAlignment="Right"/>
<TextBox Grid.Row="0" Grid.Column="2" Name="TextBoxAngle" Text="{Binding Path=Angle, Converter={StaticResource DoubleConverter}, ConverterCulture=de-de, Mode=TwoWay, StringFormat=N1}" TextAlignment="Right"/>
</Grid>
</DataTemplate>
XAML2:
<StackPanel>
<Grid>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="5"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
<ColumnDefinition Width="2*"></ColumnDefinition>
<ColumnDefinition Width="3*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right">Length</Label>
<Label Grid.Row="0" Grid.Column="2" HorizontalAlignment="Right">Angle</Label>
</Grid>
<ItemsControl x:Name="LbLegs" ItemTemplate="{DynamicResource LegTemplate}" />
<Button Content="AddLeg" Click="Button_Click_2"></Button>

How to change a grid layout when orientation is changed

I'm creating a win8 app and I need to change the layout of my grid so everything fits on screen when the user flips between orientations. I understand I need to use VisualStateManager but I can't understand any tutorials. If I have this code:
<Grid>
<Button x:Name="button1" Margin="188,73,286,0" VerticalAlignment="Top"/>
<Button x:Name="button2" Margin="236,73,238,0" VerticalAlignment="Top"/>
<Button x:Name="button3" Margin="284,73,190,0" VerticalAlignment="Top"/>
</Grid>
How would I use visual state manager to change the buttons so they are now orientated in a column (one above the other) instead of in a row when the orientation is changed to portrait from landscape?
Thanks
xaml code
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" SizeChanged="Page_SizeChanged_1">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid x:Name="FullScreenLandscape">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Content="Button1" HorizontalAlignment="Center" VerticalAlignment="Center"></Button>
<Button Content="Button2" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center"></Button>
<Button Content="Button3" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center"></Button>
</Grid>
<Grid x:Name="Snapped">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Content="Button1" HorizontalAlignment="Center" VerticalAlignment="Center"></Button>
<Button Content="Button2" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center"></Button>
<Button Content="Button3" Grid.Row="2" HorizontalAlignment="Center" VerticalAlignment="Center"></Button>
</Grid>
</Grid>
C# code
private void Page_SizeChanged_1(object sender, SizeChangedEventArgs e)
{
switch (ApplicationView.Value)
{
case ApplicationViewState.FullScreenLandscape:
VisualStateManager.GoToState(this, "FullScreenLandscape", false);
Snapped.Visibility = Visibility.Collapsed;
break;
case ApplicationViewState.Snapped:
VisualStateManager.GoToState(this, "Snapped", false);
FullScreenLandscape.Visibility = Visibility.Collapsed;
Snapped.Visibility = Visibility.Visible;
break;
default:
return;
}
}
Another Method
xaml code
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid x:Name="landscape">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox x:Name="t1" Grid.Column="0" FontSize="24" Height="100"/>
<TextBox x:Name="t2" Grid.Column="1" FontSize="24" Height="100"/>
<TextBox x:Name="t3" Grid.Column="2" FontSize="24" Height="100"/>
</Grid>
<Grid x:Name="snap" Visibility="Collapsed">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
</Grid>
</Grid>
C# code
private void Page_SizeChanged_1(object sender, SizeChangedEventArgs e)
{
if (Windows.UI.ViewManagement.ApplicationView.Value == Windows.UI.ViewManagement.ApplicationViewState.FullScreenLandscape)
{
landscape.Visibility = Windows.UI.Xaml.Visibility.Visible;
snap.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
t1.SetValue(Grid.ColumnProperty, 0);
t2.SetValue(Grid.ColumnProperty, 1);
if (t1.Parent != landscape)
{
snap.Children.Remove(t1);
snap.Children.Remove(t2);
landscape.Children.Add(t1);
landscape.Children.Add(t2);
}
}
else if(Windows.UI.ViewManagement.ApplicationView.Value == Windows.UI.ViewManagement.ApplicationViewState.Snapped)
{
landscape.Children.Remove(t1);
landscape.Children.Remove(t2);
landscape.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
snap.Visibility = Windows.UI.Xaml.Visibility.Visible;
t1.SetValue(Grid.RowProperty, 0);
t2.SetValue(Grid.RowProperty, 1);
if (t1.Parent != snap)
{
landscape.Children.Remove(t1);
landscape.Children.Remove(t2);
snap.Children.Add(t1);
snap.Children.Add(t2);
}
}
}

Categories