如何将数据绑定到字符串而不是标签?

时间:2019-07-17 00:24:36

标签: c# xamarin xamarin.forms 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;
                });

但是,对于我的项目,我希望能够将两个属性格式化为单独的字体属性。

我想到的解决方案可能给我最大的作用是使用格式化的字符串格式化两个字符串属性,并设置标签的formattedText,如下所示。但是,我无法检索字符串中的绑定。我可以想到的另一种解决方案是为每个绑定使用单独的标签,并使用堆栈布局将它们组合在一起,但是该解决方案在我的整个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
                };

2 个答案:

答案 0 :(得分:3)

这不是您设置binding in code的方式。 {Binding ...}语法适用于XAML。要在代码中使用

var span = new Span() { ... };
span.SetBinding(Span.TextProperty, "BookName");

答案 1 :(得分:0)

Jason建议的解决方案是正确的。您可以使用它来达到您的要求。

关于, 迪内什·巴布·亚达夫(Dinesh Babu Yadav) [Syncfusion小组]