简体   繁体   English

如何绑定到绑定所在的窗口?

[英]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.

相关问题 XAML data.binding-如何将方法中的属性绑定到视图中的经度/纬度? - XAML data.binding - how can I bind properties in my method to the longitude/latitude in my view? 每次加载子页面时,母版页中的DropDownList都会绑定-如何避免这种绑定? - DropDownList in my masterpage gets bind every time childpage loads - How can I avoid that binding? 绑定:如何绑定另一个 class 的属性 - Binding: how can I bind a Property from another class 如何让我的MultiSelectList绑定到我的数据模型? - How can I get my MultiSelectList to bind to my data model? 我怎么知道我的应用程序窗口是前台窗口 - How can I tell my application window is the foreground window 如何将我的MVC模型绑定到其他表 - How can I bind my MVC Model to a different table 在Umbraco中,如何将对象的通用列表绑定到我的视图? - In Umbraco, how can I bind a generic list of Objects to my View? 如何将模板中的控件命令绑定到ViewModel - How can I bind a command of a control in template to my ViewModel 如何绑定我的列表 <string> 到datagridView? - How can I bind my List<string> to a datagridView? 如何绑定具有复杂对象属性的对象集合? - How can I bind my collection of objects with complex object attribute?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM