2017-03-21 10 views
0

私は次のXAMLを持っている:ListViewで選択した項目の太字のラベルですか?

<?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:local="clr-namespace:StyleLabelInViewCell" x:Class="StyleLabelInViewCell.MainPage"> 

    <ContentPage.Resources> 
     <ResourceDictionary> 
      <local:MainVM x:Key="MainVm" /> 
      <local:IsSelectedToStyle x:Key="IsSelectedToStyle" /> 
      <Style x:Key="SelectedStyle" TargetType="Label"> 
       <Setter Property="FontAttributes" Value="Bold" /> 
      </Style> 
     </ResourceDictionary> 
    </ContentPage.Resources> 

    <StackLayout BindingContext="{StaticResource MainVm}"> 
     <ListView x:Name="ChildListView" VerticalOptions="Center" ItemsSource="{Binding Chidren}" SelectedItem="{Binding SelectedVm}"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <ViewCell x:Name="ChildViewCell"> 
         <ViewCell.View> 
          <Grid> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="Auto" /> 
            <ColumnDefinition Width="*" /> 
            <ColumnDefinition Width="Auto" /> 
           </Grid.ColumnDefinitions> 

           <Button Text="&lt;" /> 

           <Label 
            Grid.Column="1" 
            Text="{Binding Name}" 
            FontAttributes="{Binding Source={x:Reference ChildListView}, Path=SelectedItem.Id, Converter={StaticResource IsSelectedToStyle}, ConverterParameter={Binding Path=Id}}" 
           /> 

           <Button Grid.Column="2" Text="&gt;" /> 
          </Grid> 
         </ViewCell.View> 
        </ViewCell> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
    </StackLayout> 
</ContentPage> 

し、次のコード:

using System; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Globalization; 
using Xamarin.Forms; 

namespace StyleLabelInViewCell { 
    public class MainVM : INotifyPropertyChanged { 
     public MainVM() { 
      Chidren = new ObservableCollection<ChildVM> { 
       new ChildVM(1, "Item 1"), 
       new ChildVM(2, "Item 2"), 
       new ChildVM(3, "Item 3"), 
       new ChildVM(4, "Item 4"), 
       new ChildVM(5, "Item 5"), 
       new ChildVM(6, "Item 6") 
      }; 
     } 

     public ObservableCollection<ChildVM> Chidren { get; } 

     private ChildVM selectedVm; 
     public ChildVM SelectedVm { 
      get { return selectedVm; } 
      set { 
       if (!ReferenceEquals(selectedVm, value)) { 
        selectedVm = value; 
        OnPropertyChanged(nameof(SelectedVm)); 
       } 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     protected virtual void OnPropertyChanged(string propertyName) { 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    public class ChildVM { 
     public ChildVM() { } 

     public ChildVM(int id, string name) { 
      Id = id; 
      Name = name; 
     } 

     public int Id { get; } 

     public string Name { get; } 
    } 

    public sealed class IsSelectedToStyle : IValueConverter { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { 
      var selectedId = (int)value; 
      var currentItemId = (int)parameter; // This ends up being Xamarin.Forms.Binding instance 

      return selectedId == currentItemId ? Application.Current.MainPage.Resources["SelectedStyle"] : null; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { 
      throw new NotSupportedException(); 
     } 
    } 
} 

ItemTemplateは、現在選択されている行を胸が張り裂けるされたとき、私は大胆なラベルにしようとしています。問題は、ConverterParameterXamarin.Forms.Bindingインスタンスとして送信され、別の整数が必要な場合です。

バインディングの代わりに値を取得する方法はありますか、そうでない場合はバインディングから取得する方法がありますか?あるいは、私がやろうとしていることを達成する別の方法がありますか?

+0

'ConverterParameter = {Binding Path =。}'を使ってみてください。あなたはおそらくオブジェクト全体を取得しますが、そこからIDを抽出することができます –

+0

@GeraldVersluisでした。私はまだBindingインスタンスを取得します。 – Andy

答えて

0

このpostをご覧ください。

repoを更新しました。今、あなたが行を選択すると、選択したプロパティはVM

public MyModel SelectedItem 
    { 
     get { return _selectedItem; } 
     set 
     { 
      if (_selectedItem != null) 
       _selectedItem.Selected = false; 

      _selectedItem = value; 

      if (_selectedItem != null) 
       _selectedItem.Selected = true; 
     } 
    } 

とIValueConverter FontAttribute

にブール「選択」プロパティを変換に設定されている

<ListView SelectedItem="{Binding SelectedItem}" ItemsSource="{Binding List}"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
     <ViewCell> 
      <Label Text="{Binding Name}" FontAttributes="{Binding Selected, Converter={StaticResource cnvInvertFontAttribute}}}" TextColor="{Binding Selected, Converter={StaticResource cnvInvert}}}" FontSize="18"></Label> 
     </ViewCell> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

FontAttributesプロパティのバインディングもあります

public class SelectedToFontAttributeConverter : IValueConverter 
{ 

    #region IValueConverter implementation 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     if (value is bool) 
     { 
      if ((Boolean)value) 
       return FontAttributes.Bold; 
      else 
       return FontAttributes.None; 
     } 
     return FontAttributes.None; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 

    #endregion 
} 
関連する問題