问题:使用按钮将 Picker 选定项传递给 ViewModel 时出现问题。

huangapple go评论66阅读模式
英文:

Problem passing Picker SelectedItem to ViewModel using a Button

问题

以下是您要翻译的内容:

I have a Picker that is populated with a list of years using data binding. The picker works fine. I used some Labels to confirm that the SelectedIndex and SelectedItem were getting set properly (see image). The problem is when I try to pass the SelectedItem value to the ViewModel using a Button, it's not getting set.

Image showing that the SelectedIndex and SelectedItem are getting set

I have tried the following but it seems like the value is not being passed as a string. "SelectedYear" is null. Any help is much appreciated!

View:

<CollectionView ItemsSource="{Binding Dates}">
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="model:Date">
                <VerticalStackLayout>
                    <Picker x:Name="picker"
                            ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:SummaryStatViewModel}}, Path=Dates}"
                            ItemDisplayBinding="{Binding Year}"
                            Title="{Binding Year}"
                            TitleColor="White"
                            TextColor="White"
                            BackgroundColor="{StaticResource Gray600}"
                            Opacity="25"
                            HorizontalTextAlignment="Center"
                            FontSize="Medium"
                            Margin="5,5,5,5">
                    </Picker>
                    <Border HeightRequest="50"
                            Stroke="Black">
                        <VerticalStackLayout>
                        <Label Text="{Binding Source={x:Reference picker}, Path=SelectedIndex}" TextColor="Black"/>
                        <Label Text="{Binding Source={x:Reference picker}, Path=SelectedItem.Year}" TextColor="Black"/>
                        </VerticalStackLayout>
                    </Border>
                </VerticalStackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

        <Button Text="Get Results"
                TextColor="White"
                Grid.Row="1"
                WidthRequest="200"
                BackgroundColor="{StaticResource Primary}"
                BorderColor="Black"
                BorderWidth="1"
                Margin="0,0,0,0"
                Command="{Binding GetSummaryStatsCommand}"
                CommandParameter="{Binding Source={x:Reference picker}, Path=SelectedItem.Year}"/>

ViewModel:

[QueryProperty("SelectedYear", "SelectedYear")]
    
    public partial class SummaryStatViewModel : BaseViewModel
	{
        [ObservableProperty]
        string selectedYear;

请注意,我已经删除了不需要翻译的代码部分。如果您需要进一步的帮助或有其他问题,请随时提出。

英文:

I have a Picker that is populated with a list of years using data binding. The picker works fine. I used some Labels to confirm that the SelectedIndex and SelectedItem were getting set properly (see image). The problem is when I try to pass the SelectedItem value to the ViewModel using a Button, it's not getting set.

Image showing that the SelectedIndex and SelectedItem are getting set

I have tried the following but it seems like the value is not being passed as a string. "SelectedYear" is null. Any help is much appreciated!

View:

<CollectionView ItemsSource="{Binding Dates}">
        <CollectionView.ItemTemplate>
            <DataTemplate x:DataType="model:Date">
                <VerticalStackLayout>
                    <Picker x:Name="picker"
                            ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:SummaryStatViewModel}}, Path=Dates}"
                            ItemDisplayBinding="{Binding Year}"
                            Title="{Binding Year}"
                            TitleColor="White"
                            TextColor="White"
                            BackgroundColor="{StaticResource Gray600}"
                            Opacity="25"
                            HorizontalTextAlignment="Center"
                            FontSize="Medium"
                            Margin="5,5,5,5">
                    </Picker>
                    <Border HeightRequest="50"
                            Stroke="Black">
                        <VerticalStackLayout>
                        <Label Text="{Binding Source={x:Reference picker}, Path=SelectedIndex}" TextColor="Black"/>
                        <Label Text="{Binding Source={x:Reference picker}, Path=SelectedItem.Year}" TextColor="Black"/>
                        </VerticalStackLayout>
                    </Border>
                </VerticalStackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

        <Button Text="Get Results"
                TextColor="White"
                Grid.Row="1"
                WidthRequest="200"
                BackgroundColor="{StaticResource Primary}"
                BorderColor="Black"
                BorderWidth="1"
                Margin="0,0,0,0"
                Command="{Binding GetSummaryStatsCommand}"
                CommandParameter="{Binding Source={x:Reference picker}, Path=SelectedItem.Year}"/>

ViewModel:

[QueryProperty("SelectedYear", "SelectedYear")]
    
    public partial class SummaryStatViewModel : BaseViewModel
	{
        [ObservableProperty]
        string selectedYear;

答案1

得分: 0

将按钮放在CollectionView内部解决了这个问题。

更新的视图:

<CollectionView ItemsSource="{Binding Dates}">
    <CollectionView.ItemTemplate>
        <DataTemplate x:DataType="model:Date">
            <VerticalStackLayout>
                <Picker x:Name="picker"
                        ItemsSource="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:SummaryStatViewModel}}, Path=Dates}"
                        ItemDisplayBinding="{Binding Year}"
                        Title="{Binding Year}"
                        TitleColor="White"
                        TextColor="White"
                        BackgroundColor="{StaticResource Gray600}"
                        Opacity="25"
                        HorizontalTextAlignment="Center"
                        FontSize="Medium"
                        Margin="5,5,5,10">
                </Picker>
                
                <Button Text="获取结果"
                        TextColor="White"
                        WidthRequest="200"
                        BackgroundColor="{StaticResource Primary}"
                        BorderColor="Black"
                        BorderWidth="1"
                        Margin="0,0,0,5"
                        Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:SummaryStatViewModel}}, Path=GetSummaryStatsCommand}"
                        CommandParameter="{Binding Source={x:Reference picker}, Path=SelectedItem.Year}"/>
            </VerticalStackLayout>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>
英文:

Placing the Button inside the CollectionView fixed it.

Updated View:

&lt;CollectionView ItemsSource=&quot;{Binding Dates}&quot;&gt;
    &lt;CollectionView.ItemTemplate&gt;
        &lt;DataTemplate x:DataType=&quot;model:Date&quot;&gt;
            &lt;VerticalStackLayout&gt;
                &lt;Picker x:Name=&quot;picker&quot;
                        ItemsSource=&quot;{Binding Source={RelativeSource AncestorType={x:Type viewmodel:SummaryStatViewModel}}, Path=Dates}&quot;
                        ItemDisplayBinding=&quot;{Binding Year}&quot;
                        Title=&quot;{Binding Year}&quot;
                        TitleColor=&quot;White&quot;
                        TextColor=&quot;White&quot;
                        BackgroundColor=&quot;{StaticResource Gray600}&quot;
                        Opacity=&quot;25&quot;
                        HorizontalTextAlignment=&quot;Center&quot;
                        FontSize=&quot;Medium&quot;
                        Margin=&quot;5,5,5,10&quot;&gt;
                &lt;/Picker&gt;
                
                &lt;Button Text=&quot;Get Results&quot;
                        TextColor=&quot;White&quot;
                        WidthRequest=&quot;200&quot;
                        BackgroundColor=&quot;{StaticResource Primary}&quot;
                        BorderColor=&quot;Black&quot;
                        BorderWidth=&quot;1&quot;
                        Margin=&quot;0,0,0,5&quot;
                        Command=&quot;{Binding Source={RelativeSource AncestorType={x:Type viewmodel:SummaryStatViewModel}}, Path=GetSummaryStatsCommand}&quot;
                        CommandParameter=&quot;{Binding Source={x:Reference picker}, Path=SelectedItem.Year}&quot;/&gt;
                    
                
            &lt;/VerticalStackLayout&gt;
        &lt;/DataTemplate&gt;
    &lt;/CollectionView.ItemTemplate&gt;
&lt;/CollectionView&gt;

huangapple
  • 本文由 发表于 2023年6月16日 00:59:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483946.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定