[英]Binding Visibility without XAML
I am quite new to WPF, and especially to DataBinding, and it is pretty hard for me to understand, how to code it without XAML. 对于WPF,尤其是对DataBinding来说,我是一个陌生的人,很难理解如何在不使用XAML的情况下对其进行编码。 How can I bind parents visibility (parent is scrollViewer) to its child visibility (child is grid), without XAML?
如何在不使用XAML的情况下将父级可见性(父级为scrollViewer)与其子级可见性(子级为网格)绑定在一起? Here is what I am trying to do right now:
这是我现在正在尝试做的事情:
{
//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);
}
And here what is my "Vis" right now: 现在,我的“可见”是什么:
public Visibility Vis
{
get
{
return Visibility;
}
set
{
Visibility = value;
OnPropertyChanged(Visibility);
}
}
Ofcourse, right now problem is OnPropertyChanged
, it gives me an error, and I need to put DependencyPropertyChangedEventArgs
inside, but I do not know how to. 当然,现在的问题是
OnPropertyChanged
,它给了我一个错误,我需要将DependencyPropertyChangedEventArgs
放入其中,但我不知道该怎么做。 I am not sure, am I doing this right way (maybe this can be simplified?), or maybe my method will not work at all, even after fixing this error. 我不确定,我这样做是否正确(也许可以简化?),或者即使修复了此错误,我的方法也根本无法工作。 I need it to be without XAML, because controls are created dynamically.
我需要没有XAML,因为控件是动态创建的。
Solution provided by PieterWitvoet worked for me. PieterWitvoet提供的解决方案为我工作。 I had to set
scrollViewer
visibility to Visibility.Visible
on its creation, but everything else works just fine. 我必须将
scrollViewer
可见Visibility.Visible
设置为Visibility.Visible
在其创建时Visibility.Visible
,但其他一切都很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.