Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 按钮在CollectionView中不可单击_C#_Xamarin_Button_Xamarin.android_Collectionview - Fatal编程技术网

C# 按钮在CollectionView中不可单击

C# 按钮在CollectionView中不可单击,c#,xamarin,button,xamarin.android,collectionview,C#,Xamarin,Button,Xamarin.android,Collectionview,我有一个CollectionView和我的自定义按钮。我想用按钮制作一个网格。当我点击按钮时,它会改变背景色。我想在CollectionViewSelectionChanged(object sender,SelectionChangedEventArgs e)上空白处写下一些东西,标签的文本是名称(类字段)所选按钮的。当我单击collectionview中的按钮时,它会改变颜色,但按钮不可单击,它看不到它,如果我写入图像,它可以读取数据。请帮助我使按钮可单击 <StackLayout&g

我有一个CollectionView和我的自定义按钮。我想用按钮制作一个网格。当我点击按钮时,它会改变背景色。我想在CollectionViewSelectionChanged(object sender,SelectionChangedEventArgs e)上空白处写下一些东西,标签的文本是名称(类字段)所选按钮的。当我单击collectionview中的按钮时,它会改变颜色,但按钮不可单击,它看不到它,如果我写入图像,它可以读取数据。请帮助我使按钮可单击

<StackLayout>
                <Label  x:Name="meow1"></Label>
                <CollectionView  ItemsSource="{Binding Cars}" x:Name="phonesList" 
                         HeightRequest="90"
                         ItemsLayout="HorizontalList"
                        
                         BackgroundColor="Transparent"
                         SelectionMode="Single"
                         SelectionChanged="OnCollectionViewSelectionChanged">

                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                        <Frame x:Name="frame" CornerRadius="10"  BackgroundColor="Black" Padding="0"    HeightRequest="90"
                       WidthRequest="95">
                                <Grid Padding="0" x:Name="meow">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                <controls:CustomButton TintColor="#725762" HeightRequest="90"
                                                         WidthRequest="90" CornerRadius="10" HorizontalOptions="Center" 
                                                         BackgroundColor="White" ImageSource="{Binding ImagePath}" Clicked="Button_OnClicked"/>
                            </Grid>
                            </Frame>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>

            </StackLayout>

     void OnCollectionViewSelectionChanged(object sender, SelectionChangedEventArgs e)
            {
               meow1.Text = (e.CurrentSelection.FirstOrDefault() as Car).NameImage;
        

}

CollectionViewSelectionChanged无效(对象发送者,SelectionChangedEventArgs e)
{
meow1.Text=(e.CurrentSelection.FirstOrDefault()作为Car);
}

虽然不太了解这个问题,但有一个建议是关于
按钮
集合视图中单击事件
。我们将在绑定模型时使用。这就是的设计思想

例如,Xaml代码模式化如下:

<StackLayout>
    <Label x:Name="meow1" 
           Text="{Binding SelectedCarItem.NameImage}"
           FontSize="Large"
           VerticalOptions="Start" 
           HorizontalOptions="CenterAndExpand" />
    <CollectionView  ItemsSource="{Binding Cars}"
                        x:Name="phonesList"
                        HeightRequest="90"
                        ItemsLayout="HorizontalList"
                        BackgroundColor="Transparent"
                        SelectionMode="Single"
                        SelectedItem="{Binding SelectedCarItem}">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Frame x:Name="frame"
                        CornerRadius="10"
                        BackgroundColor="{Binding BgFrameColor}"
                        Padding="0"
                        HeightRequest="90"
                        WidthRequest="95">
                    <Grid Padding="0"
                            x:Name="meow">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Button 
                            HeightRequest="90"
                            WidthRequest="90"
                            CornerRadius="10"
                            HorizontalOptions="Center"
                            BackgroundColor="{Binding BgButtonColor}"
                            ImageSource="{Binding ImagePath}"
                            Command="{Binding TapCommand}"
                            CommandParameter="{Binding Source={x:Reference frame}, Path=BindingContext}" />
                    </Grid>
                </Frame>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</StackLayout>
添加CarModel类以加载数据:

public class CarModel: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public List<Car> Cars { get; set; }

    //public static Car SelectedCarItem { set; get; }
    public CarModel()
    {
        Cars = new List<Car>();
        Cars.Add(new Car() { NameImage = "Lexus", ImagePath = "Lexus.png", BgButtonColor = Color.White, BgFrameColor = Color.Black, IsSelected = false }); ;
        Cars.Add(new Car { NameImage = "Audi", ImagePath = "Audi.png", BgButtonColor = Color.White, BgFrameColor = Color.Black, IsSelected = false });
        // set default text of label 
        selectedCarItem = new Car() { NameImage = "Welcome to the car home!" };
    }

    private Car selectedCarItem;
    public Car SelectedCarItem
    {
        get
        {
            return selectedCarItem;
        }
        set
        {
            if (selectedCarItem != value)
            {
                selectedCarItem = value;
                OnPropertyChanged("SelectedCarItem");
            }
        }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
效果如下:


注意:从示例中,您将看到使用绑定修改
背景色
和模型数据。因此,不建议使用CollectionViewSelectionChanged中的
来修改
标签的文本

,尽管对该问题了解不多,但有一个建议是关于
集合视图中的
按钮
单击事件
。我们将在绑定模型时使用。这就是的设计思想

例如,Xaml代码模式化如下:

<StackLayout>
    <Label x:Name="meow1" 
           Text="{Binding SelectedCarItem.NameImage}"
           FontSize="Large"
           VerticalOptions="Start" 
           HorizontalOptions="CenterAndExpand" />
    <CollectionView  ItemsSource="{Binding Cars}"
                        x:Name="phonesList"
                        HeightRequest="90"
                        ItemsLayout="HorizontalList"
                        BackgroundColor="Transparent"
                        SelectionMode="Single"
                        SelectedItem="{Binding SelectedCarItem}">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Frame x:Name="frame"
                        CornerRadius="10"
                        BackgroundColor="{Binding BgFrameColor}"
                        Padding="0"
                        HeightRequest="90"
                        WidthRequest="95">
                    <Grid Padding="0"
                            x:Name="meow">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition Width="Auto" />
                        </Grid.ColumnDefinitions>
                        <Button 
                            HeightRequest="90"
                            WidthRequest="90"
                            CornerRadius="10"
                            HorizontalOptions="Center"
                            BackgroundColor="{Binding BgButtonColor}"
                            ImageSource="{Binding ImagePath}"
                            Command="{Binding TapCommand}"
                            CommandParameter="{Binding Source={x:Reference frame}, Path=BindingContext}" />
                    </Grid>
                </Frame>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</StackLayout>
添加CarModel类以加载数据:

public class CarModel: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public List<Car> Cars { get; set; }

    //public static Car SelectedCarItem { set; get; }
    public CarModel()
    {
        Cars = new List<Car>();
        Cars.Add(new Car() { NameImage = "Lexus", ImagePath = "Lexus.png", BgButtonColor = Color.White, BgFrameColor = Color.Black, IsSelected = false }); ;
        Cars.Add(new Car { NameImage = "Audi", ImagePath = "Audi.png", BgButtonColor = Color.White, BgFrameColor = Color.Black, IsSelected = false });
        // set default text of label 
        selectedCarItem = new Car() { NameImage = "Welcome to the car home!" };
    }

    private Car selectedCarItem;
    public Car SelectedCarItem
    {
        get
        {
            return selectedCarItem;
        }
        set
        {
            if (selectedCarItem != value)
            {
                selectedCarItem = value;
                OnPropertyChanged("SelectedCarItem");
            }
        }
    }

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
效果如下:


注意:从示例中,您将看到使用绑定修改
背景色
和模型数据。因此,不建议使用CollectionViewSelectionChanged中的
来修改
标签的文本

您不能按名称引用模板中的元素。您需要修改元素绑定到的基础模型对象。如果您想在单击按钮时执行操作,则需要使用Clicked处理程序,而不是SelectionChanged。SelectionChanged将触发单元格中的任何点击事件,而不仅仅是按钮点击。按钮点击的代码在哪里?当你点击按钮时,它会开火吗?你的问题是“按钮不可点击”,但在你的评论中,它说“它工作”。我不明白你要解决的实际问题是什么。@MariaKamenskyh嗨,欢迎来到SO!你能分享一张图片来解释这个问题吗?这将清楚地理解你想要什么。你不能通过名称引用模板中的元素。您需要修改元素绑定到的基础模型对象。如果您想在单击按钮时执行操作,则需要使用Clicked处理程序,而不是SelectionChanged。SelectionChanged将触发单元格中的任何点击事件,而不仅仅是按钮点击。按钮点击的代码在哪里?当你点击按钮时,它会开火吗?你的问题是“按钮不可点击”,但在你的评论中,它说“它工作”。我不明白你要解决的实际问题是什么。@MariaKamenskyh嗨,欢迎来到SO!你能分享一张图片来解释这个问题吗,这将清楚地了解你想要什么。小姜-MSFT,太棒了!!!!非常感谢!谢谢!谢谢@MariaKamenskyh很高兴能帮上忙!请不要忘记接受它作为答案(单击✔ 在这个答案的左上角),它将帮助其他有类似问题的人。:-)小姜-MSFT,太棒了!!!!非常感谢!谢谢!谢谢@MariaKamenskyh很高兴能帮上忙!请不要忘记接受它作为答案(单击✔ 在这个答案的左上角),它将帮助其他有类似问题的人。:-)