[英]How can I bind to the window my binding is in?
I have a data-binding that relies on the window in which it is defined. 我有一个数据绑定依赖于定义它的窗口。
Trying to create the binding like thus : 试图像这样创建绑定:
<Binding Source="{RelativeSource Self}"/>
results in an error. 导致错误。
I want the binding to resolve to the window itself... how can I accomplish this? 我希望绑定解析为窗口本身...如何完成此操作?
This has been marked as a duplicate of a couple of questions, however the method described therein is the method I am trying here and it is not working. 这被标记为几个问题的重复,但是其中描述的方法是我在这里尝试的方法,并且不起作用。
I am making use of a MultiConverter that I wrote which expects two bindings - one to a boolean, and one to a window : 我正在使用我编写的MultiConverter,它期望两个绑定-一个绑定到布尔值,一个绑定到窗口:
<MultiBinding Converter="{c:MyMultiConverter}">
<MultiBinding.ConverterParameter>
<sys:Int32>0</sys:Int32>
</MultiBinding.ConverterParameter>
<!--This binding works fine-->
<Binding Path="MyBooleanProperty" Source="{x:Static MyPropertySource}"/>
<!--This binding results in an error - 'Value cannot be null.'-->
<Binding Source="{RelativeSource Self}"/>
</MultiBinding>
This is the gist of the converters Convert function : 这是转换器Convert函数的要点:
public object Convert(
object[ ] values, Type targetType,
object parameter, CultureInfo culture ) {
int
//Get the monitor number the window is currently on...
Monitor = Screen.AllScreens.ToList( ).IndexOf( ( values[1] as Window ).Screen( ) ),
//[0] : If true, multiply by 2; else by 1. Add Parameter.
Index = ( Monitor * ( ( bool ) values[0] ? 2 : 1 ) ) + ( int )parameter;
return MyProject.MyList[Index];
}
Window.Screen( )
is just a simple extension method which returns the screen the window is located on. Window.Screen( )
只是一个简单的扩展方法,它返回窗口所在的屏幕。
Debugging reveals that attempting to resolve values[1] as Window
results in null
... 调试显示,尝试将Value values[1] as Window
解析values[1] as Window
导致null
...
{Binding RelativeSource={RelativeSource AncestorType=Window}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.