我是WPF的新手,尤其是DataBinding,我很难理解,如何在没有XAML的情况下编写代码。如何在不使用XAML的情况下将父级可见性(父级是scrollViewer)绑定到其子可见性(子级是网格)?这就是我现在要做的事情:
{
//Code that creates grid
//Code that creates scrollViewer
scrollViewer.Content = grid;
LayoutRoot.Children.Add(scrollViewer); //adding it to the main window
//Creating binding
Binding myBinding = new Binding("Vis");
BooleanToVisibilityConverter c = new BooleanToVisibilityConverter();
myBinding.Source = grid.Visibility;
myBinding.Converter = c;
myBinding.Mode = BindingMode.TwoWay;
myBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
BindingOperations.SetBinding(scrollViewer, VisibilityProperty, myBinding);
}
这就是我的" Vis"现在:
public Visibility Vis
{
get
{
return Visibility;
}
set
{
Visibility = value;
OnPropertyChanged(Visibility);
}
}
当然,现在问题是OnPropertyChanged
,它给了我一个错误,我需要把DependencyPropertyChangedEventArgs
放进去,但我不知道怎么做。我不确定,我这样做是否正确(也许这可以简化?),或者我的方法根本无法工作,即使修复了这个错误。我需要它没有XAML,因为控件是动态创建的。
答案 0 :(得分:0)
PieterWitvoet提供的解决方案为我工作。我必须在scrollViewer
创建Visibility.Visible
时将其设置为'ascii' codec can't encode character u'\xf5' in position 14: ordinal not in range(128)
(...)
Error during template rendering
(...)
<h1 id="site-name">{{ servidor }}</h1>
,但其他一切工作正常。