【问题标题】:C# UWP Button binding with flyout not refreshing button contentC# UWP 按钮绑定与弹出不刷新按钮内容
【发布时间】:2018-06-13 08:09:25
【问题描述】:

我有一个按钮,用于显示我创建的类的值。一切正常,除了在代码中更改绑定的值后按钮内容不会刷新。如果我退出屏幕并返回,则该值是正确的。停留在同一屏幕上不会刷新按钮内容。

按钮代码如下所示。

    <Grid x:Name="Task1Grid" Grid.Row="0" Grid.Column="0" Margin="5,0,5,0">
                    <Grid.RowDefinitions>
                        <RowDefinition Height=".2*"/>
                        <RowDefinition Height=".6*"/>
                        <RowDefinition Height=".2*"/>
                    </Grid.RowDefinitions>
                    <Button Grid.Row="1" Style="{StaticResource RoundedButtonStyle}" Tag="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="StoplightButton_Click" FontFamily="Global User Interface">
                        <Button.Content>
                            <Image Stretch="Uniform" Source="{Binding SelectedRepairOrder.TaskStatusGrid[0], Converter={StaticResource TaskStatusToStopLight}, Mode=OneWay}"/>
                        </Button.Content>
                        <Button.Background>
                            <ImageBrush Stretch="Uniform" ImageSource="{Binding SelectedRepairOrder.TaskStatusGrid[0], Converter={StaticResource TaskStatusToStopLight}, Mode=OneWay}"/>
                        </Button.Background>
                    </Button>
                    <Button x:Name="Task0Time"  Tag="0" Style="{StaticResource RoundedButtonStyle}" Visibility="{Binding SelectedRepairOrder.TaskStatusGrid[0].NewTaskstatus, Converter=

{StaticResource TaskStatusToVisibility}}" IsEnabled="{Binding ShowForecastFeatures}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Content="{Binding SelectedRepairOrder.TaskStatusGrid[0].TmTimecmpltask, Converter={StaticResource TaskCompleteTimeToTime}}" Grid.Row="2" Flyout="{StaticResource Task1Flyout}"/>
                    <TextBlock Grid.Row="0" Text="{Binding ClientInfo.TasksInfo[0].TaskDescription}" TextAlignment="Center" VerticalAlignment="Bottom" FontSize="28"/>
                </Grid>

弹出代码如下所示。

    <Border x:Name="StopLightBorder" Background="CornflowerBlue" Grid.Row="1" BorderBrush="White" BorderThickness="2">
        <Grid x:Name="StopLightGrid" Margin="5" >
            <Grid.Resources>
                <converter:TaskStatusToStopLight x:Key="TaskStatusToStopLight"/>
                <converter:TaskCompleteTimeToTime x:Key="TaskCompleteTimeToTime"/>
                <converter:TaskStatusToVisibility x:Key="TaskStatusToVisibility"/>
                <Flyout x:Key="Task1Flyout" >
                    <ListBox ItemsSource="{Binding ForecastTimes}" Tag="0" SelectionChanged="ForecastTimeChanged"/>
                </Flyout>

更改绑定值的代码如下所示。

    private void ForecastTimeChanged(object sender, SelectionChangedEventArgs e)
    {
        var timeListBox = (ListBox)sender;
        var completeTime = Convert.ToDateTime(e.AddedItems[0].ToString());
        var taskNum = Convert.ToInt16(((FrameworkElement)sender).Tag);
        var result = checkPreviousTaskTimes(completeTime, taskNum);
        switch (result)
        {
            case ForecastResult.ValidTime:
                globalContext.SelectedRepairOrder.TaskStatusGrid[taskNum].TmTimecmpltask = completeTime.ToString();
                globalContext.SelectedRepairOrder.TaskStatusGrid[taskNum].DtDateoverride = completeTime.ToString();
                globalContext.SelectedRepairOrder.TaskStatusGrid[taskNum].TmTimeoverride = completeTime.ToString();
                globalContext.SelectedRepairOrder.TaskStatusGrid[taskNum].SendOverrideForecastTime = true;
                globalContext.SelectedRepairOrder.WasChanged = true;
                globalContext.SelectedRepairOrder.RecordGrid = "1";
                ((Popup)((FlyoutPresenter)((FrameworkElement)sender).Parent).Parent).IsOpen = false;
                break;
            default:
                showForecastError(result, completeTime, taskNum);
                break;
        }     
    }

Visibility 和 IsEnabled 都可以正常工作。不知道此时我还能做什么。在您离开屏幕之前,更改绑定数据似乎没有效果。我一直在追查这个问题,并看到了数据的变化以及我所期望的一切。弹出按钮会激活 forecasttimechanged 方法。当我们去把这个数据保存到数据库中时,数据是正确的。在屏幕上查看时,浮出控件会显示选定的时间,这正是我想要的。我看到浮出控件中突出显示了这一点。

如果有比按钮更好的控件可以使用,我现在全神贯注。这是棘手的部分。可以在应用程序以及您从中查看代码的应用程序中设置此预测时间。该应用程序的时间以 15 分钟为增量,但可以更新此控件的其他程序可以随时添加。

我知道需要设置一些控制或参数才能使这正确发生,但对于我的生活,我找不到它。过去 3 天我已经尝试了所有方法,但没有任何效果。

请帮帮我。

【问题讨论】:

    标签: c# visual-studio button uwp flyout


    【解决方案1】:

    我知道需要设置一些控制或参数才能使这正确发生,但对于我的生活,我找不到它。过去 3 天我已经尝试了所有方法,但没有任何效果。

    从你的代码中,我猜问题是你没有为绑定属性实现INotifyPropertyChanged。而且您的逻辑很复杂,您可以通过以下示例的简单方式实现您的功能。

    <Button  Content="{Binding SelectItem,Mode=OneWay}">
        <Button.Flyout>
            <Flyout Placement="Top">
                <ListBox ItemsSource="{Binding Items}" SelectedItem="{Binding SelectItem,Mode=TwoWay}">
                </ListBox>
            </Flyout>
        </Button.Flyout>
    </Button>
    

    将按钮内容与SelectItem绑定,如果ListBoxSelectedItem发生变化,按钮内容会自动修改。

    public class MainPageViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    
        public List<string> Items { get; set; } = new List<string>();
    
        private string selectItem = "Nico";
        public string SelectItem { get { return selectItem; } set { selectItem = value; OnPropertyChanged(); } }
    
        public MainPageViewModel()
        {
            Items.Add("Nico");
            Items.Add("Song");
            Items.Add("Xiao");
        }
    

    【讨论】:

      猜你喜欢
      • 2019-10-25
      • 2017-10-30
      • 2012-09-05
      • 2017-06-08
      • 2011-05-22
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      • 2017-04-17
      相关资源
      最近更新 更多