Search This Blog

Wednesday, July 22, 2015

Add a Border to a TextBlock WPF

WPF > Windows > Controls > Border

If you want to display a border around your content, you must place the elements within a parent Border element.

Example: Add a Border to a TextBlock

<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Left"                     VerticalAlignment="Top">
            <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap"  Text="TextBlock"  VerticalAlignment="Top"/>
 </Border>


Tuesday, July 7, 2015

Table in RichTextBox WPF C#

WPF > Controls  > ContentControl > RichTextBox > Table

XAML

<Window x:Class="Editor.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <RichTextBox>
            <FlowDocument>
                <Table>
                    <TableRowGroup>
                        <TableRow>
                            <TableCell>
                                <Paragraph>Cell1</Paragraph>
                            </TableCell>
                            <TableCell>
                                <Paragraph>Cell2</Paragraph>
                            </TableCell>
                        </TableRow>
                    </TableRowGroup>
                </Table>
            </FlowDocument>
        </RichTextBox>
    </Grid>
</Window>

Thursday, May 14, 2015

ComboBox Binding WPF C# Example

WPF > Controls > ItemsControl > ComboBox

ComboBox control  allows a user to select an item from a dropdown list.



Example

ComboBox bind to Student view model and display selected value to  TextBlock. 

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ComboBox ItemsSource="{Binding Path=Students}"
                  DisplayMemberPath="LastName"
                  SelectedValuePath="FirstName"
                  SelectedValue="{Binding Path=Student}" Margin="10,28,337,263"
                  Name="cboStudent"
                  />
        <Label Content="Student" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top"/>
        <TextBlock HorizontalAlignment="Left" Margin="10,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top"
                  Text="{Binding ElementName=cboStudent, Path=SelectedValue}" Width="170"/>
    </Grid>
</Window>

namespace WpfApplication1
{
    public class Student
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public class StudentViewModel : INotifyPropertyChanged
    {
        private readonly CollectionView students;
        private string student;

        public StudentViewModel()
        {
            IList<Student> list = new List<Student>();
            list.Add(new Student() { FirstName = "Bill" , LastName = "Jones" });
            list.Add(new Student() { FirstName = "John", LastName = "Water" });
            students = new CollectionView(list);
        }

        public string Student
        {
            get { return student; }
            set
            {
                if (student == value) return;
                student = value;
                OnPropertyChanged("student");
            }
        }
        private void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        public CollectionView Students
        {
            get { return students; }
        }

    }

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            StudentViewModel viewModel = new StudentViewModel();
            DataContext = viewModel;

        }
    }
}


Monday, January 12, 2015

Media Namespace WPF

WPF > Windows > Media

Media enable integration of rich media:

  • drawings
  • text
  • audio/video content

Classes



Extract the Text Content from a RichTextBox

WPF > Windows > Documents > TextRange

TextRange is a selection of content between two TextPointer positions.

Example

Extract the Text Content from a RichTextBox

 TextRange textRange = new TextRange(richTB.Document.ContentStart,   richTB.Document.ContentEnd);

WPF Documents Namespace

WPF > Windows > Documents

WPF Documents contains types that support FixedDocument, FlowDocument and XPS document creation. 

Thursday, November 13, 2014

Menu WPF Example

WPF > Controls > Menu

Menu presents a list of items that specify commands or options for an application. Typically, clicking an item on a menu opens a submenu or causes an application execute a command.


Example

<Window x:Class="MapDecision.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <DockPanel>
        <Menu DockPanel.Dock="Top">
            <MenuItem Header="_File">
                <MenuItem Header="_New"/>
                <MenuItem Header="_Open"/>
                <MenuItem Header="_Close"/>
                <MenuItem Header="_Save"/>
            </MenuItem>
        </Menu>
        <StackPanel></StackPanel>
    </DockPanel>
</Window>