[英]Binding Grid Column Width on Xamarin.Forms
I'm attempting to build a 3 column status bar with the Grid control in Xamarin.Forms.我正在尝试使用 Xamarin.Forms 中的网格控件构建一个 3 列状态栏。 I receive 3 values: open, pending and filled.我收到 3 个值:打开、待处理和已填充。 I would like the column width to be bound to those values.我希望列宽绑定到这些值。 However, the column widths remain equal.但是,列宽保持相等。
Current Look:当前外观:
Desired Look:所需外观:
This is achieved by replacing the bindings in the Column Width with 3*, 0*, and 2* respectively.这是通过将列宽中的绑定分别替换为 3*、0* 和 2* 来实现的。
XAML XAML
<Frame HorizontalOptions="FillAndExpand" CornerRadius="10" HasShadow="False" Padding="0" IsClippedToBounds="True">
<Grid ColumnSpacing="0" HorizontalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="25" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding Filled, StringFormat='{0:N}*'}" />
<ColumnDefinition Width="{Binding Pending, StringFormat='{0:N}*'}" />
<ColumnDefinition Width="{Binding Open, StringFormat='{0:N}*'}" />
</Grid.ColumnDefinitions>
<BoxView Grid.Column="0" Color="#5cb85c" />
<Label Grid.Column="0" Text="{Binding Filled}" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" />
<BoxView Grid.Column="1" Color="#f0ad4e" />
<Label Grid.Column="1" Text="{Binding Pending}" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" />
<BoxView Grid.Column="2" Color="#d43f3a" />
<Label Grid.Column="2" Text="{Binding Open}" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" />
</Grid>
</Frame>
UPDATE:更新:
I updated my code with your suggestion.我用你的建议更新了我的代码。 The issue is that the converter is not fired.问题是转换器没有被触发。 I do not get an error and the breakpoint in the converter is never reached.我没有收到错误,并且从未达到转换器中的断点。 I added "Source={x:Reference listView}" as I've seen some SO answers suggest it.我添加了“Source={x:Reference listView}”,因为我已经看到一些 SO 答案建议它。 However, it doesn't help.但是,它没有帮助。 I also tried to add a basic double to string converter to the label and it doesn't execute either.我还尝试在 label 中添加一个基本的双精度字符串转换器,但它也不执行。
XAML: XAML:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:conv="clr-namespace:MyApp.Converters" x:Class="MyApp.Views.OrgDash.PositionsPage" Title="Positions">
<ContentPage.Resources>
<ResourceDictionary>
<conv:NumberToGridLengthConverter x:Key="numberToGridLengthConverter" />
</ResourceDictionary>
</ContentPage.Resources>
<ContentPage.Content>
<AbsoluteLayout x:Name="absLayout" VerticalOptions="FillAndExpand">
<ListView x:Name="listView" HasUnevenRows="true" ItemTapped="OnItemTapped" HeightRequest="{Binding Path=Height, Source={x:Reference absLayout}}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="10,10,10,10" Orientation="Vertical">
<StackLayout HorizontalOptions="StartAndExpand" Orientation="Horizontal">
<Label Text="{Binding Name}" VerticalTextAlignment="Center" FontAttributes="Bold" />
</StackLayout>
<Frame HorizontalOptions="FillAndExpand" CornerRadius="10" HasShadow="False" Padding="0" IsClippedToBounds="True">
<Grid ColumnSpacing="0" HorizontalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="25" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding Filled, Source={x:Reference listView}, Converter={StaticResource numberToGridLengthConverter}}" />
<ColumnDefinition Width="{Binding Pending, Source={x:Reference listView}, Converter={StaticResource numberToGridLengthConverter}}" />
<ColumnDefinition Width="{Binding Open, Source={x:Reference listView}, Converter={StaticResource numberToGridLengthConverter}}" />
</Grid.ColumnDefinitions>
<BoxView Grid.Column="0" Color="#5cb85c" />
<Label Grid.Column="0" Text="{Binding Filled" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" />
<BoxView Grid.Column="1" Color="#f0ad4e" />
<Label Grid.Column="1" Text="{Binding Pending}" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" />
<BoxView Grid.Column="2" Color="#d43f3a" />
<Label Grid.Column="2" Text="{Binding Open}" TextColor="White" HorizontalOptions="Center" VerticalOptions="Center" />
</Grid>
</Frame>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ContentView x:Name="overlay" AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All" IsVisible="True" BackgroundColor="#C0808080" Padding="10, 0">
<ActivityIndicator WidthRequest="110" HeightRequest="70" IsRunning="True" IsVisible="True" Color="Black" HorizontalOptions="CenterAndExpand" VerticalOptions="CenterAndExpand"/>
</ContentView>
</AbsoluteLayout>
</ContentPage.Content>
</ContentPage>
Converter Class:转换器 Class:
using System;
using System.Globalization;
using Xamarin.Forms;
namespace MyApp.Converters
{
public class NumberToGridLengthConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var numberValue = (double)value;
return new GridLength(numberValue, GridUnitType.Star);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var gridLength = (GridLength)value;
return gridLength.Value;
}
}
}
I guess that your binding will cause binding error and you will see in Debug window something like this:我猜您的绑定会导致绑定错误,您将在 Debug window 中看到如下内容:
[0:] Binding: '3.00*' can not be converted to type 'Xamarin.Forms.GridLength'. [0:] 绑定:“3.00*”无法转换为类型“Xamarin.Forms.GridLength”。
[0:] Binding: '0.00*' can not be converted to type 'Xamarin.Forms.GridLength'. [0:] 绑定:“0.00*”无法转换为类型“Xamarin.Forms.GridLength”。
[0:] Binding: '2.00*' can not be converted to type 'Xamarin.Forms.GridLength'. [0:] 绑定:“2.00*”无法转换为类型“Xamarin.Forms.GridLength”。
I would use a custom converter to convert number value to GrindLength
:我会使用自定义转换器将数值转换为GrindLength
:
public class NumberToGridLengthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var numberValue = (double)value;
return new GridLength(numberValue, GridUnitType.Star);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var gridLength = (GridLength)value;
return gridLength.Value;
}
}
Xaml: Xaml:
<ContentPage.Resources>
<ResourceDictionary>
<conv:NumberToGridLengthConverter x:Key="numberToGridLengthConverter" />
</ResourceDictionary>
</ContentPage.Resources>
...
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding Filled, Converter={StaticResource numberToGridLengthConverter}}" />
<ColumnDefinition Width="{Binding Pending, Converter={StaticResource numberToGridLengthConverter}}" />
<ColumnDefinition Width="{Binding Open, Converter={StaticResource numberToGridLengthConverter}}" />
</Grid.ColumnDefinitions>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.