Mega Code Archive

 
Categories / C# / WPF
 

SizeToContent=WidthAndHeight

<Window x:Class="CustomDialogSample.SettingsDialog"   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   Title="Settings"   Height="200"   Width="400"   ResizeMode="CanResizeWithGrip"   SizeToContent="WidthAndHeight"   WindowStartupLocation="CenterOwner"   FocusManager.FocusedElement="{Binding ElementName=myStringTextBox}"   ShowInTaskbar="False">   <StackPanel>     <Label Target="{Binding ElementName=myStringTextBox}">Report _Folder</Label>     <TextBox x:Name="myStringTextBox" Text="{Binding MyString}" />     <Button x:Name="folderBrowseButton">...</Button>     <Button HorizontalAlignment="Left" Name="reportColorButton">       <StackPanel>       <Rectangle Width="15" Height="15" SnapsToDevicePixels="True"                    Fill="{StaticResource reportBrush}" />         <AccessText Text="Report _Color..." Margin="10,0,0,0" />       </StackPanel>     </Button>     <Button x:Name="okButton" IsDefault="True">OK</Button>     <Button x:Name="cancelButton" IsCancel="True">Cancel</Button>   </StackPanel> </Window> //File:Window.xaml.cs using System; using System.Collections.Generic; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.ComponentModel; namespace CustomDialogSample {     class DialogData : INotifyPropertyChanged {       Color reportColor;       public Color MyColor {         get { return reportColor; }         set { reportColor = value; Notify("MyColor"); }       }       string mystring;       public string MyString {         get { return mystring; }         set { mystring = value; Notify("MyString"); }       }       public event PropertyChangedEventHandler PropertyChanged;       void Notify(string prop) { if( PropertyChanged != null ) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); } }     }   public partial class SettingsDialog : System.Windows.Window {     DialogData data = new DialogData();     public Color MyColor {       get { return data.MyColor; }       set { data.MyColor = value; }     }     public string MyString {       get { return data.MyString; }       set { data.MyString = value; }     }     public SettingsDialog() {       InitializeComponent();       DataContext = data;       reportColorButton.Click += reportColorButton_Click;       folderBrowseButton.Click += folderBrowseButton_Click;       okButton.Click += new RoutedEventHandler(okButton_Click);     }     void okButton_Click(object sender, RoutedEventArgs e) {       DialogResult = true;     }     void reportColorButton_Click(object sender, RoutedEventArgs e) {       System.Windows.Forms.ColorDialog dlg = new System.Windows.Forms.ColorDialog();       Color color = MyColor;       dlg.Color = System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);       if( dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK ) {         MyColor = Color.FromArgb(dlg.Color.A, dlg.Color.R, dlg.Color.G, dlg.Color.B);       }     }     void folderBrowseButton_Click(object sender, RoutedEventArgs e) {       System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();       dlg.SelectedPath = MyString;       if( dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK ) {         MyString = dlg.SelectedPath;       }     }   } }