2016-09-05 3 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"); 

редактирования: кроме того, вы не имеете 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(); 
    }   
} 
+0

Это не сработало. – rur2641

+0

отредактировал ответ, извините, я не замечаю, что вы делаете это совершенно неправильно - привязка без ViewModel;) – RTDev

+0

Thx alot! Оно работает. – rur2641

Смежные вопросы