英文:
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:
<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="Get Results"
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论