WPF: XamlParserException for a very simple form?
I don't believe this: just built a very simple single list form, when the user selects one item, the selection will be displayed on the label. Here is my code:
<Window x:Class="WpfApplication8.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<ComboBox Height="23" Margin="139,0,19,14" Name="comboBox1" Text="Worker"
VerticalAlignment="Bottom" IsReadOnly="True" SelectionChanged="comboBox1_SelectionChanged">
<ComboBoxItem Name="None" Selector.IsSelected="True">Select Target</ComboBoxItem>
<ComboBoxItem Name="Alice">Alice</ComboBoxItem>
<ComboBoxItem Name="Bob">Bob</ComboBoxItem>
<ComboBoxItem Name="Chris">Chris</ComboBoxItem>
<ComboBoxItem Name="Dan">Dan</ComboBoxItem>
</ComboBox>
<Label Height="28" Margin="15,0,0,14" Name="label1"
VerticalAlignment="Bottom" Content="Assign to: " HorizontalAlignment="Left" Width="120"></Label>
</Grid>
</Window>
Code behind:
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
label1.Content = comboBox1.SelectedValue;
}
0
a source to share
3 answers
Another way also, if you don't want to check if the label is null, add a selection change handler after loading the window, since it runs it before loading the label:
Code for:
public Window1()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Window1_Loaded);
}
void Window1_Loaded(object sender, RoutedEventArgs e)
{
comboBox1.SelectionChanged+=new SelectionChangedEventHandler(comboBox1_SelectionChanged);
}
protected void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
label1.Content = ((ComboBoxItem)comboBox1.Items[comboBox1.SelectedIndex]).Content;
}
Markup:
<Grid>
<ComboBox Height="23" Margin="139,0,19,14" Name="comboBox1" Text="Worker"
VerticalAlignment="Bottom" IsReadOnly="True">
<ComboBoxItem Name="None" Selector.IsSelected="True">Select Target</ComboBoxItem>
<ComboBoxItem Name="Alice">Alice</ComboBoxItem>
<ComboBoxItem Name="Bob">Bob</ComboBoxItem>
<ComboBoxItem Name="Chris">Chris</ComboBoxItem>
<ComboBoxItem Name="Dan">Dan</ComboBoxItem>
</ComboBox>
<Label Height="28" Margin="15,0,0,14" Name="label1"
VerticalAlignment="Bottom" Content="Assign to: " HorizontalAlignment="Left" Width="120"></Label>
</Grid>
Andrew
0
a source to share
Here's a simplified version of everything in xaml:
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<ComboBox Name="comboBox1" Text="Worker" IsSynchronizedWithCurrentItem="True"
VerticalAlignment="Bottom" IsReadOnly="True" >
<ComboBoxItem Name="None" Selector.IsSelected="True">Select Target</ComboBoxItem>
<ComboBoxItem Name="Alice">Alice</ComboBoxItem>
<ComboBoxItem Name="Bob">Bob</ComboBoxItem>
<ComboBoxItem Name="Chris">Chris</ComboBoxItem>
<ComboBoxItem Name="Dan">Dan</ComboBoxItem>
</ComboBox>
<Label Name="label1" DataContext="{Binding ElementName=comboBox1, Path=SelectedItem}"
VerticalAlignment="Bottom" Content="{Binding Name}" HorizontalAlignment="Left"></Label>
</StackPanel>
</Page>
+1
a source to share