简体   繁体   English

Xamarin Forms 使用自定义 ControlTemplate 覆盖 Picker

[英]Xamarin Forms override Picker with custom ControlTemplate

I want to override the Xamarin Forms Control Picker so it display "No entries" if the binded collection is empty.我想覆盖 Xamarin Forms 控件选取器,以便在绑定集合为空时显示“无条目”。

I tried:我试过:

<Picker
    x:Class="v.App.Styling.Controls.vPicker"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:d="http://xamarin.com/schemas/2014/forms/design"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:controls="clr-namespace:v.App.Styling.Controls;assembly=v.App"
    x:Name="CustomPicker"
    mc:Ignorable="d" >

    <Picker.Style>
        <Style TargetType="controls:vPicker">
            <Setter Property="ControlTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <controls:vStackLayout >
                            <ContentPresenter 
                                IsVisible="{Binding ItemsSource.Count, Converter={StaticResource HasItemsToBoolConverter}, ConverterParameter=0}"
                                Content="{TemplateBinding Content}" />

                            <controls:vEntry 
                                IsVisible="{Binding ItemsSource, Converter={StaticResource HasNoItemsToBoolConverter}"
                                IsEnabled="False"
                                TextColor="{StaticResource ColorTextSecondary}"
                                Text="No entries"  />
                        </controls:vStackLayout>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Picker.Style>
</Picker>

But I get但我明白了

      Error XFC0001: Cannot resolve property "ControlTemplate" on type "vPicker (property missing or missing accessors)". (13, 21)

I'm not sure what I'm doing wrong...我不确定我做错了什么......

you could use data bindings to make it.您可以使用数据绑定来实现它。

In the.xaml for Picker:在.xaml for Picker中:

<Picker x:Name="picker"
        ItemsSource="{Binding ItemCollection}"
        IsVisible="{Binding IsVisible}"
        Title="Select a monkey"
        TitleColor="Red">         
</Picker> 

And in ViewModel just add some logic like this:在 ViewModel 中只需添加一些这样的逻辑:

public Command ClickedCommand  // i define a button to add item to ItemCollection as a demo
{
    get
    {
        return new Command(() =>
        {
            ItemCollection.Add(count);
            count++;
            if(ItemCollection.Count > 0)
            {
                IsVisible = true;
            }
        });
    }
}

Hope it works for you.希望对你有效。

Another solution that I offered someone trying to create their own style, but for a button.我为尝试创建自己的样式的人提供的另一个解决方案,但用于按钮。 The underlying premise is the same.基本前提是相同的。 You have something special you want to do based on a default class (or derived).基于默认的 class(或派生的),您有一些特别的事情想要做。

I would take the picker control, subclass it and add your own dependency property boolean or visibility so it can be used within your own custom style template.我会采用选择器控件,将其子类化并添加您自己的依赖属性 boolean 或可见性,以便它可以在您自己的自定义样式模板中使用。 A dependency property is instance specific, so if you had one picker that HAD entries, its flag would be different than another that had NO entries.依赖属性是特定于实例的,所以如果你有一个有条目的选择器,它的标志将不同于另一个没有条目的选择器。

Now, that said, Here is one source to start about your own templates / styles现在,就是说, 这是一个开始您自己的模板的来源 / styles

But this one goes into an elaborate control template for a picker.但这一个进入了一个精心设计的选择器控制模板。 You could build your own simplified and have your visible vs not context defined once as a style.您可以构建自己的简化版,并将可见与不可见的上下文定义为一种样式。 Then when you define your combobox picker control, just have it reference the given style you declare.然后,当您定义 combobox 选择器控件时,只需让它引用您声明的给定样式即可。 Having this style in a ResourceDictionary and available system wide can help all instances have the same look and feel without duplicating all over.在 ResourceDictionary 中使用这种样式并在整个系统范围内可用可以帮助所有实例具有相同的外观和感觉,而不会完全重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM