如何将数据绑定到字符串而不是标签?
How to bind data to a string instead of label?
我有来自 SyncFusion 的以下代码,它将 属性 书 BookName
绑定到标签。
listView = new SfListView() { ItemSpacing = 5 };
listView.WidthRequest = 350;
listView.ItemTemplate = new DataTemplate(() =>
{
ViewCell viewCell = new ViewCell();
var grid = new Grid() { RowSpacing = 1 };
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = 50 });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = 200 });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = 50 });
var contactImage = new Image()
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
HeightRequest = 50
};
contactImage.SetBinding(Image.SourceProperty, new Binding("ContactImage"));
var contactName = new Label()
{
HorizontalTextAlignment = TextAlignment.Center,
LineBreakMode = LineBreakMode.NoWrap,
FontSize = Font.SystemFontOfSize(NamedSize.Medium).FontSize,
};
contactName.SetBinding(Label.TextProperty, new Binding("ContactName"));
var contactType = new Image()
{
VerticalOptions = LayoutOptions.End,
HorizontalOptions = LayoutOptions.End,
HeightRequest = 50,
};
contactType.SetBinding(Image.SourceProperty, new Binding("ContactType"));
grid.Children.Add(contactImage, 0, 0);
grid.Children.Add(contactName, 1, 0);
grid.Children.Add(contactType, 2, 0);
viewCell.View = grid;
return viewCell;
});
但是,对于我的项目,我希望能够将我的两个属性格式化为单独的字体属性。
我想到的最有效的解决方案是使用格式化字符串格式化两个字符串属性并设置标签的格式化文本,如下所示。但是,我无法检索字符串中的绑定。我能想到的另一个解决方案是为每个绑定设置单独的标签并使用堆栈布局组合它们,但该解决方案在我的 UI 中并不一致。提前致谢!
var unformattedBookName = new Binding("BookName");
var unformattedBookDescription = new Binding("BookDescription");
var formattedString = new FormattedString();
formattedString.Spans.Add(new Span { Text = "{Binding unformattedBookName}", ForegroundColor = Color.Red, FontAttributes = FontAttributes.Bold });
formattedString.Spans.Add(new Span { Text = "italic small.", FontAttributes = FontAttributes.Italic, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)) });
var bookName = new Label()
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Start,
HeightRequest = 100,
FontAttributes = FontAttributes.Bold,
FontSize = 16,
FormattedText = formattedString
}
编辑:最终解决方案
正如@Jason 所建议的,我能够将两个绑定合并为一个并将我的格式化字符串设置为单个标签。 =)
var formattedString = new FormattedString();
var bookSpan = new Span
{
ForegroundColor = Color.Red,
FontAttributes = FontAttributes.Bold
};
bookSpan.SetBinding(Span.TextProperty, "BookName");
var bookDescriptionSpan = new Span
{
ForegroundColor = Color.Red,
FontAttributes = FontAttributes.Italic,
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label))
};
bookDescriptionSpan.SetBinding(Span.TextProperty, "BookDescription");
formattedString.Spans.Add(bookSpan);
formattedString.Spans.Add(bookDescriptionSpan);
var combineBookLabel = new Label()
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Start,
HeightRequest = 100,
FontAttributes = FontAttributes.Bold,
FontSize = 16,
FormattedText = formattedString
};
这不是您设置 binding in code 的方式。 {Binding ...}
语法适用于 XAML。要在代码中使用
var span = new Span() { ... };
span.SetBinding(Span.TextProperty, "BookName");
杰森建议的解决方案是正确的。您可以使用相同的方法来实现您的要求。
此致,
迪内什·巴布·亚达夫
[Syncfusion 团队]
我有来自 SyncFusion 的以下代码,它将 属性 书 BookName
绑定到标签。
listView = new SfListView() { ItemSpacing = 5 };
listView.WidthRequest = 350;
listView.ItemTemplate = new DataTemplate(() =>
{
ViewCell viewCell = new ViewCell();
var grid = new Grid() { RowSpacing = 1 };
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = 50 });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = 200 });
grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = 50 });
var contactImage = new Image()
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
HeightRequest = 50
};
contactImage.SetBinding(Image.SourceProperty, new Binding("ContactImage"));
var contactName = new Label()
{
HorizontalTextAlignment = TextAlignment.Center,
LineBreakMode = LineBreakMode.NoWrap,
FontSize = Font.SystemFontOfSize(NamedSize.Medium).FontSize,
};
contactName.SetBinding(Label.TextProperty, new Binding("ContactName"));
var contactType = new Image()
{
VerticalOptions = LayoutOptions.End,
HorizontalOptions = LayoutOptions.End,
HeightRequest = 50,
};
contactType.SetBinding(Image.SourceProperty, new Binding("ContactType"));
grid.Children.Add(contactImage, 0, 0);
grid.Children.Add(contactName, 1, 0);
grid.Children.Add(contactType, 2, 0);
viewCell.View = grid;
return viewCell;
});
但是,对于我的项目,我希望能够将我的两个属性格式化为单独的字体属性。
我想到的最有效的解决方案是使用格式化字符串格式化两个字符串属性并设置标签的格式化文本,如下所示。但是,我无法检索字符串中的绑定。我能想到的另一个解决方案是为每个绑定设置单独的标签并使用堆栈布局组合它们,但该解决方案在我的 UI 中并不一致。提前致谢!
var unformattedBookName = new Binding("BookName");
var unformattedBookDescription = new Binding("BookDescription");
var formattedString = new FormattedString();
formattedString.Spans.Add(new Span { Text = "{Binding unformattedBookName}", ForegroundColor = Color.Red, FontAttributes = FontAttributes.Bold });
formattedString.Spans.Add(new Span { Text = "italic small.", FontAttributes = FontAttributes.Italic, FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label)) });
var bookName = new Label()
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Start,
HeightRequest = 100,
FontAttributes = FontAttributes.Bold,
FontSize = 16,
FormattedText = formattedString
}
编辑:最终解决方案 正如@Jason 所建议的,我能够将两个绑定合并为一个并将我的格式化字符串设置为单个标签。 =)
var formattedString = new FormattedString();
var bookSpan = new Span
{
ForegroundColor = Color.Red,
FontAttributes = FontAttributes.Bold
};
bookSpan.SetBinding(Span.TextProperty, "BookName");
var bookDescriptionSpan = new Span
{
ForegroundColor = Color.Red,
FontAttributes = FontAttributes.Italic,
FontSize = Device.GetNamedSize(NamedSize.Small, typeof(Label))
};
bookDescriptionSpan.SetBinding(Span.TextProperty, "BookDescription");
formattedString.Spans.Add(bookSpan);
formattedString.Spans.Add(bookDescriptionSpan);
var combineBookLabel = new Label()
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Start,
HeightRequest = 100,
FontAttributes = FontAttributes.Bold,
FontSize = 16,
FormattedText = formattedString
};
这不是您设置 binding in code 的方式。 {Binding ...}
语法适用于 XAML。要在代码中使用
var span = new Span() { ... };
span.SetBinding(Span.TextProperty, "BookName");
杰森建议的解决方案是正确的。您可以使用相同的方法来实现您的要求。
此致, 迪内什·巴布·亚达夫 [Syncfusion 团队]