2016-09-05 80 views
0

嗨,我正在遵循本教程http://blogs.u2u.be/diederik/post/2011/11/14/null.aspx,将元素的可见性绑定到布尔属性。该程序无法正常工作。以下是代码:麻烦绑定XAML uwp

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

可见性不会随着属性而改变。由于intellisense(Error Xaml namespace),我解决了错误。不知道这段代码有什么问题。

谢谢。

回答

1

变化

this.OnPropertyChanged(); 

this.OnPropertyChanged("Show_element"); 

编辑: 除此之外,你没有一个视图模型(对不起,错过了,当我检查你的代码),所以你需要创建一个并将其设置为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(); 
    }   
} 
+0

它没有工作。 – rur2641

+0

编辑了答案,对不起,我没有注意到你完全错了 - 没有ViewModel绑定;) – RTDev

+0

Thx很多!有用。 – rur2641