卡夫卡的阿鲸 2024-03-06 14:26 采纳率: 44.4%
浏览 24
已结题

wpf程序中Binding绑定值未显示

wpf程序想要实现点击按钮会显示值,但是如图代码并没有显示值

img

img

是必须使用set{SetProperty(ref _value,value)}的写法才会显示吗?但是另一个程序界面值会发生实时变化,哪位能帮忙看一下两种区别是什么

img

img

  • 写回答

2条回答 默认 最新

  • 关注

    卡夫卡的阿鲸 下午好🌅🌅🌅
    本答案参考ChatGPT-3.5

    Based on the provided code snippets, it seems that you are trying to display a value in a WPF program by clicking a button. However, the value is not being displayed properly.

    Here are a few possible reasons for this issue:

    1. Missing DataContext: Make sure that the DataContext of the button or its parent control is set to the appropriate view model or data source that contains the CurrentValue property. Without the correct DataContext, the binding won't work.

    2. Binding Path: Ensure that the binding path is correct and matches the property name in the data context. In your case, the binding should be Text="{Binding CurrentValue}" instead of Text="(Binding CurrentValue)".

    3. INotifyPropertyChanged: If you are using the CurrentValue property in a two-way binding scenario (such as updating the value from the UI), implement the INotifyPropertyChanged interface in your view model class. This interface notifies the UI when the property value changes, ensuring that the updated value is displayed.

    To fix the issue, please try the following steps:

    1. Set the appropriate DataContext for the parent control or the button. You can do this by setting the DataContext property in XAML or programmatically in the code-behind.

    2. Update the binding path to Text="{Binding CurrentValue}" in the TextBlock element.

    3. Implement the INotifyPropertyChanged interface in your view model and raise the PropertyChanged event in the CurrentValue property setter. This will notify the UI of property changes.

    Here's an example of how the modified code could look:

    public class YourViewModel : INotifyPropertyChanged
    {
        private string _currentValue;
    
        public string CurrentValue
        {
            get { return _currentValue; }
            set
            {
                _currentValue = value;
                OnPropertyChanged(nameof(CurrentValue));
            }
        }
    
        public ICommand ShowValueCommand { get; }
    
        public YourViewModel()
        {
            ShowValueCommand = new DelegateCommand(async () => await ShowValueCommandAsync());
        }
    
        private async Task ShowValueCommandAsync()
        {
            CurrentValue = "study";
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    
    <StackPanel>
        <Button Width="200" Height="100" VerticalAlignment="Center" Command="{Binding ShowValueCommand}">
            <TextBlock Text="{Binding CurrentValue}" />
        </Button>
    </StackPanel>
    

    Please make sure to adjust the code according to your specific implementation and data context.

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月6日
  • 创建了问题 3月6日

悬赏问题

  • ¥15 [vasp+phonopy]表面模型的间隙扩散系数中尝试频率如何用声子频率计算
  • ¥15 现在新建了一个f1的就不行了包括所有新建的项目都无法生成,路径命名都没问题,keil和cubemx重装过还是不行,如何解决?(标签-java|关键词-固件)
  • ¥15 web前端开发怎么实现像图片这样的页面啊?
  • ¥15 ubuntu 20.04 网卡启用后,只有ipv6 没有 ipv4 无法上网
  • ¥15 QT任务管理器无法正确获取展开,有悬赏15元速求,如何解决?(相关搜索:标识符|结构体)
  • ¥15 使用delphi 10.3+intraweb 生成的页面怎么实现自动滚屏
  • ¥20 思科:Router c3600 MN-4E插槽
  • ¥15 16进制修改视频的元数据
  • ¥15 HTML中css的位置信息居然会导致元素大小发生变化
  • ¥15 岛津txt格式文件转nirs格式