Data bindings is to
separate the GUI from the data. In the case of WPF, properties are the ideal
interface between GUI and objects. Each time a property is changed, an event
will be raised, and the GUI gets notified about it. There is no need to connect
it, as this works via reflection, the only code you have to write is the
notification that a property has been changed.
The ComboBox element represents a ComboBox control in XAML.
<ComboBox></ComboBox>
The Width and Height properties
represent the width and the height of a ComboBox. The x:Name property represents the name of
the control, which is a unique identifier of a control. The Margin property sets the location of a ComboBox on the parent control. The HorizontalAlignment and VerticalAlignment properties are used
to set horizontal and vertical alignments.
Example
Below example contain
three controls TextBox, TextBlock and ComboBox. When the user
types any text inside the textbox, the text is automatically displayed in the TextBlock control, because the TextBlock
control is
data bound
to the textbox control.There is minimal effort to data-bind the two controls together, with the textbox
control being the data source.
Similarly Combobox of size and color are work they automatically change it
color and size when user are select comboBox item.
<Grid>
<Grid Width="300">
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition Height="200"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Height="20"
Text="Text:" TextWrapping="Wrap"
Grid.Row="0" Grid.Column="0"/>
<TextBlock Height="20"
Text="Color:" TextWrapping="Wrap"
Grid.Row="1" Grid.Column="0"/>
<TextBlock Height="20"
Text="Size:" TextWrapping="Wrap"
Grid.Row="2" Grid.Column="0"/>
<TextBox x:Name="txt"
Grid.Column="1" Height="20"
Width="194"></TextBox>
<ComboBox x:Name="CmbColor"
VerticalAlignment="Top" Width="194" Height="20" Grid.Row="1" Grid.Column="1">
<ComboBoxItem Content="Red"></ComboBoxItem>
<ComboBoxItem Content="Green"></ComboBoxItem>
<ComboBoxItem Content="Orange"></ComboBoxItem>
<ComboBoxItem Content="Blue"></ComboBoxItem>
<ComboBoxItem Content="pink"></ComboBoxItem>
<ComboBoxItem Content="Gray"></ComboBoxItem>
</ComboBox>
<ComboBox x:Name="Cmbsize"
VerticalAlignment="Top" Width="194" Height="20" Grid.Row="2" Grid.Column="1">
<ComboBoxItem Content="22"></ComboBoxItem>
<ComboBoxItem Content="24"></ComboBoxItem>
<ComboBoxItem Content="26"></ComboBoxItem>
<ComboBoxItem Content="28"></ComboBoxItem>
<ComboBoxItem Content="30"></ComboBoxItem>
<ComboBoxItem Content="32"></ComboBoxItem>
<ComboBoxItem Content="34"></ComboBoxItem>
<ComboBoxItem Content="36"></ComboBoxItem>
<ComboBoxItem Content="38"></ComboBoxItem>
<ComboBoxItem Content="40"></ComboBoxItem>
</ComboBox>
<TextBlock Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2">
<TextBlock.Text>
<Binding ElementName="txt"
Path="Text"/>
</TextBlock.Text>
<TextBlock.Foreground>
<Binding ElementName="CmbColor"
Path="SelectedItem.Content"/>
</TextBlock.Foreground>
<TextBlock.FontSize>
<Binding ElementName="Cmbsize"
Path="SelectedItem.Content"/>
</TextBlock.FontSize>
</TextBlock>
</Grid>
</Grid>
Read More »
Tags:
ComboBox,
DataBinding,
Example,
textBox