我不相信:只用一个组合框构建一个非常简单的表单,当用户选择一个项目时,标签将显示选择。这是我的代码:
<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>
代码背后:
private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
label1.Content = comboBox1.SelectedValue;
}
答案 0 :(得分:1)
以下是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 :(得分:0)
以下作品:
protected void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (label1 != null)
label1.Content = ((ComboBoxItem)comboBox1.Items[comboBox1.SelectedIndex]).Content;
}
安德鲁
答案 2 :(得分:0)
另一种方法是,如果您不想检查标签是否为null,则在加载窗口后添加选择更改处理程序,因为它会在加载标签之前触发一个:
代码背后:
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;
}
标记:
<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>
安德鲁