Custom Dialog with data binding : Binding « Windows Presentation Foundation « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Windows Presentation Foundation » BindingScreenshots 
Custom Dialog with data binding
  

<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) { ifPropertyChanged != 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);

      ifdlg.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;
      ifdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK ) {
        MyString = dlg.SelectedPath;
      }
    }

  }
}

   
    
  
Related examples in the same category
1.Bind property of one instantiated controlBind property of one instantiated control
2.Bind to Window itselfBind to Window itself
3.Two level path bindingTwo level path binding
4.Bind RelativeSource's AncestorTypeBind RelativeSource's AncestorType
5.Bind RelativeSource's AncestorType's PathBind RelativeSource's AncestorType's Path
6.Bind Stroke Thickness to SliderBind Stroke Thickness to Slider
7.Bind current time to ButtonBind current time to Button
8.Desktop to ControlDesktop to Control
9.Bind Label To ScrollBarBind Label To ScrollBar
10.Bind ScrollBar To LabelBind ScrollBar To Label
11.Binding With Data ContextBinding With Data Context
12.Long Binding PathLong Binding Path
13.Bind ListBox ItemsSource to DayNames property of DateTimeFormatInfoBind ListBox ItemsSource to DayNames property of DateTimeFormatInfo
14.Bind TextBlock Text to SelectedItem property of ListBoxBind TextBlock Text to SelectedItem property of ListBox
15.Bind To TextBox Back and ForthBind To TextBox Back and Forth
16.Binding FontFamily / FontSize value for current ControlBinding FontFamily / FontSize value for current Control
17.Bind to a Collection with the Master-Detail PatternBind to a Collection with the Master-Detail Pattern
18.Bind to IDataErrorInfo
19.Bind to a collectionBind to a collection
20.DataTemplate for bindingDataTemplate for binding
21.Binding Environment InfoBinding Environment Info
22.Bind to an ADO.NETDataSetBind to an ADO.NETDataSet
23.Add a value converter to a binding using XAMLAdd a value converter to a binding using XAML
24.One way and two way bindingOne way and two way binding
25.Object BindingObject Binding
26.Without BindingWithout Binding
27.Master Detail BindingMaster Detail Binding
28.Data binding using collections composed of mixed types of data.Data binding using collections composed of mixed types of data.
29.Text Data BindingText Data Binding
30.List BindingList Binding
31.Async bindingAsync binding
32.Null property bindingNull property binding
33.Binding Property with ExceptionBinding Property with Exception
34.Hierarchical Binding for three level nested objectsHierarchical Binding for three level nested objects
35.BindingOperations.GetBindingExpressionBindingOperations.GetBindingExpression
36.Collection View Source BindingCollection View Source Binding
37.Bind to enum typesBind to enum types
38.Bind to a MethodBind to a Method
39.Bind an ItemsControl to the CollectionViewSource, Set its DisplayMemberPath to display the Name propertyBind an ItemsControl to the CollectionViewSource, Set its DisplayMemberPath to display the Name property
40.Bind to the Values of an EnumerationBind to the Values of an Enumeration
41.Binding Dependency Property to TextBlockBinding Dependency Property to TextBlock
42.Bind Your Objects to UI Control with PropertyBind Your Objects to UI Control with Property
43.Implement INotifyPropertyChanged to notify the binding targets when the values of properties change.Implement INotifyPropertyChanged to notify the binding targets when the values of properties change.
44.Bind to an Existing Object InstanceBind to an Existing Object Instance
45.Digital ClockDigital Clock
46.Formatted Digital ClockFormatted Digital Clock
47.Manual Update TargetManual Update Target
48.Property changed callbackProperty changed callback
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.