绑定 XAML uwp 时遇到问题

Posted

技术标签:

【中文标题】绑定 XAML uwp 时遇到问题【英文标题】:Trouble binding XAML uwp 【发布时间】:2017-01-12 23:57:06 【问题描述】:

您好,我正在学习本教程http://blogs.u2u.be/diederik/post/2011/11/14/null.aspx,将元素的可见性绑定到布尔属性。该程序不工作。代码如下:

<Page.Resources>
    <local:BooleanToVisibilityConverter x:Key="TrueToVisibleConverter"/>
</Page.Resources>

<Grid Background="ThemeResource ApplicationPageBackgroundThemeBrush">
    <StackPanel>
        <TextBlock  Text=" Hello World" 
                    Visibility="Binding Path=Show_element, Converter=StaticResource TrueToVisibleConverter"/>
        <Button Click="Button_Click">press button</Button>
    </StackPanel>
</Grid>

public sealed partial class MainPage : Page , INotifyPropertyChanged

    private bool show_element ;
    public bool Show_element
    
        get  return show_element; 
        set
        
            show_element = value;
            this.OnPropertyChanged();
            Debug.WriteLine("Show_element value changed");
        
    
    public MainPage()
    
        this.InitializeComponent();
    

    private void Button_Click(object sender, RoutedEventArgs e)
    
        Show_element = !Show_element;
    
    public event PropertyChangedEventHandler PropertyChanged = delegate  ;
    public void OnPropertyChanged(string propertyName = null)
    
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    

public class BooleanToVisibilityConverter : IValueConverter
       
    public bool IsReversed  get; set; 

    public object Convert(object value, Type typeName, object parameter, string language)
    
        var val = System.Convert.ToBoolean(value);
        if (this.IsReversed)
        
            val = !val;
        

        if (val)
        
            return Visibility.Visible;
        

        return Visibility.Collapsed;
    

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    
        throw new NotImplementedException();
    

可见性不会随属性而改变。由于智能感知(Error Xaml namespace),我遇到了一个错误,该错误已解决。不知道这段代码有什么问题。

谢谢。

【问题讨论】:

【参考方案1】:

改变

this.OnPropertyChanged();

this.OnPropertyChanged("Show_element");

编辑: 除此之外,您没有 ViewModel(抱歉,我在检查您的代码时错过了),所以您需要创建一个并将其设置为 DataContext:

ViewModel.cs:

public class ViewModel : INotifyPropertyChanged

    private bool show_element;
    public bool Show_element
    
        get  return show_element; 
        set
        
            show_element = value;
            this.OnPropertyChanged("Show_element");
            Debug.WriteLine("Show_element value changed");
        
    
    public ViewModel()
    
    

    public void ButtonClicked()
    
        Show_element = !Show_element;
    

    public event PropertyChangedEventHandler PropertyChanged = delegate  ;
    public void OnPropertyChanged(string propertyName = null)
    
        this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    

你的 MainPage.xaml.cs 应该看起来像这样:

public sealed partial class MainPage : Page
        
    private ViewModel _viewModel;

    public MainPage()
    
        this.InitializeComponent();
        _viewModel = new ViewModel();
        DataContext = _viewModel;
    

    private void Button_Click(object sender, RoutedEventArgs e)
    
        _viewModel.ButtonClicked();
            

【讨论】:

以上是关于绑定 XAML uwp 时遇到问题的主要内容,如果未能解决你的问题,请参考以下文章

uwp StorageFile 没有得到简单的缩略图

x:绑定设计时问题

如何在 UWP XAML 上将样式触发器设置为路径绑定父状态

UWP DependencyObject 绑定 Windows.UI.Xaml.Markup.XamlParseException

无法将 CalendarDatePicker 绑定到 Xaml UWP 中的模型

具有多边形形状的 XAML UWP 按钮