這裏有一個簡單的解決方案,這將顯示四個文本框的組合框的文本:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBox Name="tb0" />
<TextBox Name="tb1" Grid.Row="1" />
<TextBox Name="tb2" Grid.Row="2" />
<TextBox Name="tb3" Grid.Row="3" />
<ComboBox Grid.Row="4">
<ComboBoxItem IsSelected="True">
<TextBlock Text="{Binding ElementName=tb0, Path=Text}" />
</ComboBoxItem>
<ComboBoxItem>
<TextBlock Text="{Binding ElementName=tb1, Path=Text}" />
</ComboBoxItem>
<ComboBoxItem>
<TextBlock Text="{Binding ElementName=tb2, Path=Text}" />
</ComboBoxItem>
<ComboBoxItem>
<TextBlock Text="{Binding ElementName=tb3, Path=Text}" />
</ComboBoxItem>
</ComboBox>
</Grid>
編輯: 隱藏在下拉列表中的項目時,他們空,你可以使用一個轉換器並檢查文本是否爲空。
轉換器:
public abstract class BaseConverter : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
public class StringToVisibilityConverter : BaseConverter, IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value.ToString() != string.Empty ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
添加轉換器作爲一種資源:
<Window.Resources>
<TestProject:StringToVisibilityConverter x:Key="stringToVisibilityConverter" />
</Window.Resources>
使用轉換器:
<ComboBoxItem>
<TextBlock Text="{Binding ElementName=tb3, Path=Text}" Visibility="{Binding ElementName=tb3, Path=Text, Converter={StaticResource stringToVisibilityConverter}}" />
</ComboBoxItem>
感謝answer.I注意到ComboBox顯示爲空項在其下拉列表中。接下來是讓它不顯示空白的項目。歡迎提出建議。 – kirchhoff 2012-03-01 05:15:01
我編輯答案來隱藏空的組合框項。 – Eirik 2012-03-01 07:48:10