[英]Remove 'None' from textbox without using codebehind
我有一個使用multibindings生成的運輸標簽,如下所示:
<TextBox x:Name="TextBoxShippingLabel" Margin="0,10,-2,2" TextWrapping="Wrap">
<TextBox.Text>
<MultiBinding StringFormat="{}{0} {1}
{2}
{3}
{4}
{5}
{6} {7}">
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[FirstName]" />
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[Surname]" />
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[Department]" />
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[Organisation]" />
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[Street]" />
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[Suburb]" />
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[State]" />
<Binding ElementName="dataGridOutstandingOrders" Path="SelectedItem[Postcode]" />
</MultiBinding>
</TextBox.Text>
</TextBox>
這很有效,除非組織或部門等數據回復為“無”(在個人訂單的情況下會發生)。 當發生這種情況時,標簽表示如下:
有沒有辦法我可以使用XAML來識別綁定何時返回'None'並使用備用StringFormat
?
我有同樣的問題,我使用了轉換器。 簡單干凈:
<MultiBinding Converter="{StaticResource TextAlternateConverter}">
public class TextAlternateConverter: IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
StringBuilder myOutputText = new StringBuilder();
foreach (string param in values)
{
if (param == "None")
myOutputText.Append("Give alternate text");
else
myOutputText.Append(param);
}
return myOutputText.ToString();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
我知道你要求沒有代碼隱藏的解決方案。 但是我不認為這是可能的,因為你想在你的字符串格式中有一些“ifs”。 這在后面的代碼中也是不可能的(參見我提供的擴展方法)。
如果你可以擴展SelectedItem
(無論可能是什么),我會在那里放置一個屬性。 這可能在將來有用(例如,通過API發送標簽)。 如果無法訪問代碼庫,也可以使用ExtensionMethod
。
您至少有2個“變通辦法”:
代碼背后:
public partial class MainWindow
{
public MainWindow()
{
this.InitializeComponent();
List<OrderViewModel> newList = new List<OrderViewModel>();
newList.Add(new OrderViewModel() { FirstName = "foo", LastName = "bar", Organization = "SO", ZipCode = "666" });
newList.Add(new OrderViewModel() { LastName = "bar", Organization = "SO", ZipCode = "666" });
newList.Add(new OrderViewModel() { FirstName = "foo", ZipCode = "666" });
newList.Add(new OrderViewModel() { FirstName = "foo" });
newList.Add(new OrderViewModel() { FirstName = "foo", LastName = "bar", Organization = "SO", ZipCode = "666" });
DataContext = newList;
}
}
public static class Extensions
{
public static string GenerateShippingLabel(this OrderViewModel order)
{
StringBuilder sb = new StringBuilder();
if (order.FirstName != "None")
{
sb.AppendFormat("{0} ", order.FirstName);
}
if (order.LastName != "None")
{
sb.AppendLine(order.LastName);
}
else
{
sb.AppendLine();
}
if (order.Organization != "None")
{
sb.AppendLine(order.Organization);
}
if (order.ZipCode != "None")
{
sb.AppendLine(order.ZipCode);
}
return sb.ToString();
}
}
public class ShippingLabelConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is OrderViewModel)
{
return (value as OrderViewModel).GenerateShippingLabel();
}
return "None"; //isn't it ironic? ;-)
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class OrderViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Organization { get; set; }
public string ZipCode { get; set; }
public string ShippingLabel
{
get
{
return this.GenerateShippingLabel();
}
}
}
XAML
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=ShippingLabel, Mode=OneWay}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ItemsControl ItemsSource="{Binding}" Grid.Column="2">
<ItemsControl.Resources>
<local:ShippingLabelConverter x:Key="labelConverter" />
</ItemsControl.Resources>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding Converter={StaticResource labelConverter},Mode=OneWay}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.