【问题标题】:Custom BackgroundConverter for LongListSelectorLongListSelector 的自定义 BackgroundConverter
【发布时间】:2015-01-16 22:15:30
【问题描述】:

我的应用程序中有一个特定的配色方案,我不希望我的 LongListSelectors 跳转列表样式与我的配色方案完全匹配,我已经为 JumpList 创建了样式,就像这样。

<Style x:Name="LibraryJumpListStyle" TargetType="phone:LongListSelector">
    <Setter Property="ItemTemplate">
        <Setter.Value>
            <DataTemplate>
                <Border Background="#007fb3" Margin="6" toolkit:TiltEffect.IsTiltEnabled="True">
                    <TextBlock Text="{Binding Key}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" FontSize="48" Margin="12,0,0,0" Foreground="White" VerticalAlignment="Bottom"/>
                </Border>
            </DataTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="LayoutMode" Value="Grid"/>
    <Setter Property="GridCellSize" Value="111,111"/>
    <Setter Property="Margin" Value="12,6,0,0"/>
</Style>

但这使我所有的跳转列表项目,包括禁用(我没有任何项目的部分)都具有相同的颜色。我希望禁用的跳转列表项目具有不同的颜色。我知道为此我需要制作自己的 BackgroundConverter。但问题是我不知道从哪里开始或如何开始。我需要传递什么参数。我知道 valuse Converters 是如何工作的,并且自己编写了一些转换器。但在这里我不知道。我已经搜索并没有找到任何可以用作自定义 BackgroundConverter 基础的示例代码。有人可以帮忙吗?

【问题讨论】:

    标签: c# xaml windows-phone-8 windows-phone ivalueconverter


    【解决方案1】:

    嘿,KasunKV,您是否考虑过隐藏空组?像这样,

    <phone:LongListSelector x:Name="accounts_ls" IsGroupingEnabled="True" HideEmptyGroups="true"/>
    

    如果您对禁用的颜色死心塌地,那么看看这个;代码太多,这里就不重复了:

    Change-the-background-of-a-border-with-converter

    如果您为您的组使用 GroupKeyList,那么您的 IsPair 将需要检查组的大小,并在大小为空 (0) 时返回一种颜色,否则返回另一种颜色。

    【讨论】:

      【解决方案2】:

      使用这个。

      using Windows.UI;
      using Windows.UI.Xaml.Media;
      
      public class ColorResolver : IValueConverter
      {
      
          public object Convert(object value, Type targetType, object parameter, string language)
          {
             return GetColorFromHexa((string) value)); 
          }
      
          public object ConvertBack(object value, Type targetType, object parameter, string language)
          {
              throw new NotImplementedException();
          }
      
          private SolidColorBrush GetColorFromHexa(string hexaColor)
          {
              return new SolidColorBrush(
                  Color.FromArgb(
                      255,
                      Convert.ToByte(hexaColor.Substring(1, 2), 16),
                      Convert.ToByte(hexaColor.Substring(3, 2), 16),
                      Convert.ToByte(hexaColor.Substring(5, 2), 16)
                  )
              );
           }
      }
      

      绑定要应用到边框的颜色。

      <Border Background="{Binding MyColorProperty, Converter={StaticResource MyColorConverter}}" Margin="6" toolkit:TiltEffect.IsTiltEnabled="True">
          <TextBlock Text="{Binding Key}" FontFamily="{StaticResource PhoneFontFamilySemiBold}" FontSize="48" Margin="12,0,0,0" Foreground="White" VerticalAlignment="Bottom"/>
      </Border>
      

      这是一个 WinRT 实现。只需将 GetColorFromHexa() 方法替换为合适的 WP8 方法即可转换颜色。将方法与转换器分开,这样您就可以重新使用它。而已! :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-01
        • 2014-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多