= WPF Basic Controls = == Border == ''Border'' control with rounder corners: ''Border'' control using ''Style'' to apply rounded corners: ''Border'' with header region: ''Border'' with some style: Data Here... ''Border'' with ''DropShadowEffect'': == Button or ToggleButton == Standard button: Button with gradient background style: ... A ''Button'' with shadow bitmap effect: A button style: Possible bitmap effects: * ''DropShadowEffect'' * ''BlurEffect'' * [[http://msdn.microsoft.com/en-us/library/ms752092.aspx|Bitmap Effects How-to Topics]] * [[http://www.designerwpf.com/2008/02/08/wpf-drop-shadows-on-the-cheap|WPF Drop Shadows on the Cheap]] Reflection effect: For programmatically clicking on a button: myButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent)); Tooltips on a button: More Creative: * [[swdev:dotnet:Drop Down Button]] == Cursor == Changing cursors programmatically (eg: pen, cross, scroll, etc.): this.Cursor = Cursors.None; Using custom cursors: this.Cursor = new Cursor("CustomCursorImage.jpg"); Using custom cursors via XAML: ... Or paint directly to a ''Canvas'': == Polygon == Creating polygons with behavior. In this example, some triangles working as thumbnails: Supporting C# code: private void thumbBinauralUp_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { // Do work here MessageBox.Show("Binaural Up"); } private void thumbLeftUp_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { // Do work here MessageBox.Show("Left Up"); } == ProgressBar== ''ProgressBar'' example using dynamic resources: == RadioButton == To group several ''RadioButton''s, make them part of the same group: To determine which ''RadioButton'' was selected in a group: private void RadioButton_Click(object sender, RoutedEventArgs e) { if( btnMem1.IsChecked == true) { MessageBox.Show(btnMem1.Content.ToString()); } else if (btnMem2.IsChecked == true) { MessageBox.Show(btnMem2.Content.ToString()); } else if (btnMem3.IsChecked == true) { MessageBox.Show(btnMem3.Content.ToString()); } else { MessageBox.Show(btnMem4.Content.ToString()); } } == Rectangle == A ''Rectangle'' geometric figure: == Slider == ''Slider'' example: And some sample code to handle the slider changes: void slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e) { double value = e.NewValue; ImageBrush imageBrush = this.FindName("imageBrush") as ImageBrush; imageBrush.Viewbox = new Rect(0.3, 0.3, 1 / value, 1 / value); } References: * [[http://nayyeri.net/slider-control-in-windows-presentation-foundation|Slider Control in WPF]] * [[http://codingsense.wordpress.com/2010/02/01/customize-a-slider-in-wpf-step-by-step-tutorial/|Customize a Slider in WPF]] == TabControl == ''TabControl'' with ''TabItem'' on the right side: References * [[http://stackoverflow.com/questions/189370/how-to-put-wpf-tab-control-tabs-on-the-side|How to put WPF tab control tabs on the side]] == TextBlock == ''TextBlock'' with linebreak: Hello World! ''TextBlock'' with space and newline preservation: Hello World! ''TextBlock'' with truncated text showing tooltip displaying full text : This is a long sentence with text. Alternatively, use a behavior: [[http://tranxcoder.wordpress.com/2008/10/12/customizing-lookful-wpf-controls-take-2|Customizing "Lookful" WPF controls]] == TextBox == ''TextBox'' that has auto highlighted text when control gains focus: Code to highlight: ///---------------------------------------------------------------------------------------- /// /// Highlight text in a textbox control when control gains focus. /// /// /// ///---------------------------------------------------------------------------------------- private void txtbox_GotFocus(object sender, RoutedEventArgs e) { //textbox.SelectionStart = 0; //textbox.SelectionLength = textbox.Text.Length; ((TextBox)sender).SelectionStart = 0; // Start selection at beginning. ((TextBox)sender).SelectionLength = ((TextBox)sender).Text.Length; // Length of text in Text1. } ''TextBox'' with a scrollbar: This is a TextBox == TreeView == private void treeFittingOperations_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs e) { TreeViewItem tviSelected = (TreeViewItem)treeFittingOperations.SelectedItem; // activate relevant parent tabPageControl tab if (tabControlMain != null) { // disable tabs tabPatientContactInfo.Visibility = Visibility.Collapsed; tabPatientAudiologicalData.Visibility = Visibility.Collapsed; tabProducts.Visibility = Visibility.Collapsed; tabFitting.Visibility = Visibility.Collapsed; tabDatalog.Visibility = Visibility.Collapsed; tabReports.Visibility = Visibility.Collapsed; tabFinish.Visibility = Visibility.Collapsed; tabControlMain.Items.Clear(); //MessageBox.Show(tviSelected.Header.ToString()); //debug // activate relevant tab(s) switch (tviSelected.Header.ToString()) { case "Datalog": tabControlMain.Items.Add(tabDatalog); //tabDatalog.Visibility = Visibility.Visible; break; case "Finish": tabControlMain.Items.Add(tabFinish); //tabDatalog.Visibility = Visibility.Visible; break; default: tabControlMain.Items.Add(tabFitting); //tabFitting.Visibility = Visibility.Visible; break; } } // activate relevant child tabPageControl tab switch (tviSelected.Header.ToString()) { case "Read Instrument": MessageBox.Show("Simulation: Instrument was read successfully"); break; case "Autofit": case "Initial Fit": tabControlFitting.SelectedItem = tabAutofits; break; case "Solutions": tabControlFitting.SelectedItem = tabSolutionsGuide; break; case "Quick Adjustments": tabControlFitting.SelectedItem = tabQuickAdjustments; break; case "Fine Adjustments": tabControlFitting.SelectedItem = tabFineAdjustments; break; case "Gain / Equalizers": tabControlFitting.SelectedItem = tabEqualizers; break; case "Frequency Cut-off": tabControlFitting.SelectedItem = tabFitFreqCutOff; break; case "Compression": tabControlFitting.SelectedItem = tabFitCompression; break; case "Expansion": tabControlFitting.SelectedItem = tabFitExpansion; break; case "Advanced": tabControlFitting.SelectedItem = tabFitAdvanced; break; case "Adaptive": tabControlFitting.SelectedItem = tabFitAdaptive; break; case "Indicators": tabControlFitting.SelectedItem = tabFitIndicators; break; case "Other": tabControlFitting.SelectedItem = tabFitOther; break; case "Datalog": //tabControlFitting.SelectedItem = tabDatalog; break; case "In Situ Verification": case "Verification": tabControlFitting.SelectedItem = tabInSituVerification; break; } // Refresh Tabs (call again, otherwise tab selection does not refresh. // Used to work in .NET 3.5, but stopped with .NET 4.0). tabControlFitting.BeginInit(); tabControlFitting.EndInit(); tabControlMain.BeginInit(); tabControlMain.EndInit(); } == Timer == In WPF, ''Timer'' runs on a separate thread from the UI thread, so you need to use ''DispatcherTimer'' instead: void ActivateTimer() { // using System.Windows.Threading; DispatcherTimer timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromSeconds(10); // 10 seconds timer.Tick += new EventHandler(timer_Tick); // Subscribe to Tick event handler timer.Start(); } void timer_Tick(object sender, EventArgs e) { // Do something every tick } == URI == To create a ''URI'' programmatically: // Absolute URI (default) Uri absoluteUri = new Uri("pack://application:,,,/File.xaml", UriKind.Absolute); // Relative URI Uri relativeUri = new Uri("/File.xaml", UriKind.Relative); More information: [[http://msdn.microsoft.com/en-us/library/aa970069.aspx|MSDN: Pack URIs in WPF]] == WebBrowser == A simple ''WebBrowser'' control: Clicking a button to load a web page: private void btnWebsite_Click(object sender, RoutedEventArgs e) { // External Website HtmlBrowserReport.Navigate(new Uri("http://www.example.com", UriKind.RelativeOrAbsolute)); } private void btnViewReport_Click(object sender, RoutedEventArgs e) { // Local web page // NOTE: Use siteoforigin for local files. Only files relative to application folder can be opened // No GET parameters can be passed through URI. // More Reading: http://www.dotnetfunda.com/articles/article840-working-with-webbrowser-in-wpf.aspx //HtmlBrowserReport.Navigate(new Uri("pack://siteoforigin:,,,/htmlpage1.htm", UriKind.RelativeOrAbsolute)); HtmlBrowserReport.Navigate(new Uri("pack://siteoforigin:,,,/reports/report.htm", UriKind.RelativeOrAbsolute)); // located in subdir "reports" } Execute a JavaScript routine from C#: (see more: [[http://www.dotnetfunda.com/articles/article840-working-with-webbrowser-in-wpf.aspx|Working with WebBrowser in WPF]]) public class TSampleApp { // Fields private WebBrowser aWebBrowser; . . . // Constructor public TSampleApp() { aWebBrowser = new WebBrowser(); aWebBrowser.LoadCompleted -= new LoadCompletedEventHandler(aWebBrowser_LoadCompleted); aWebBrowser.LoadCompleted += new LoadCompletedEventHandler(aWebBrowser_LoadCompleted); } public void LoadWebPage(string aWebPage) { // Load a webpage using Source property or Navigate() method // Load an external website aWebBrowser.Navigate("http://www.example.com"); aWebBrowser.Navigate(new Uri("http://www.example.com", UriKind.RelativeOrAbsolute)); // Load a local file aWebBrowser.Navigate("Products/Catalog/prodspec.htm"); aWebBrowser.Source = new Uri(@"pack://siteoforigin:,,,/Products/Catalog/prodspec.htm", UriKind.RelativeOrAbsolute); } void aWebBrowser_LoadCompleted(object sender, NavigationEventArgs e) { // Call JavaScript routine after the document has been loaded completely // Example: //aWebBrowser.InvokeScript("getAlert"); // call javascript function with no params //aWebBrowser.InvokeScript("LoadDoc", new object[] { "prod_Name.pdf" }); // call javascript function with params string AppDir = System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName); string ProdSpecPDFFilePath = "file:///" + System.IO.Path.Combine(AppDir, string.Format(@"Products\Catalog\{0}.pdf", _SelectedProduct.Code)).Replace("\\", "\\\\"); aWebBrowser.InvokeScript("LoadDoc", new object[] { ProdSpecPDFFilePath }); } } Sample HTML page with JavaScript to call: This page comes from Stream == Window == A simple ''Window'': A ''Window'' without border, with custom Maximized, Minimized, Closed, Drag, and Stretch functionality: