首页 文章

C#UWP按钮绑定与弹出按钮不刷新按钮内容

提问于
浏览
0

我有一个按钮,显示我创建的类的值 . 一切正常,除了在代码中更改绑定值后按钮内容不刷新的事实 . 如果我退出屏幕并返回,则值正确 . 保持在同一屏幕上不会刷新按钮内容 .

按钮代码如下所示 .

<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天的一切,但没有任何作用 .

请帮帮我 .

1 回答

  • 0

    我知道有一些控制或参数需要设置才能使这种情况正常发生,但对于我的生活,我找不到它 . 我已经尝试了过去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 绑定按钮内容,然后如果ListBox SelectedItem 更改,则会自动修改按钮内容 .

    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");
        }
    

相关问题