简体   繁体   English

如何设置在代码隐藏中声明的依赖属性

[英]How to set Dependency Property declared in code-behind

I'm trying to bind value from ViewModel class to custom property declared in code behind Window class.我试图将值从 ViewModel 类绑定到在 Window 类后面的代码中声明的自定义属性。 What I'm getting with this example is "The member 'FullScreen' is not recognized or is not accessible."我在这个例子中得到的是“无法识别或无法访问成员‘全屏’。” error.错误。

Here is MainWindow class:这是 MainWindow 类:

public partial class MainWindow : Window
    {
        public static readonly DependencyProperty FullScreenProperty =
            DependencyProperty.Register(nameof(FullScreen), typeof(bool),
                typeof(MainWindow), new PropertyMetadata(default(bool), PropertyChangedCallback));

        public bool FullScreen
        {
            get => (bool)GetValue(FullScreenProperty);
            set => SetValue(FullScreenProperty, value);
        }

        private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var value = (bool)e.NewValue;
            //this.GoToFullScreen(value);
        }

        public MainWindow()
        {
            InitializeComponent();
        }
    }

And the XAML part:和 XAML 部分:

<Window x:Class="WindowDependencyPropertyTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WindowDependencyPropertyTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" FullScreen="{Binding FullScreenFromVm}" >
    <Grid>

    </Grid>
</Window>

What would be the best way to achieve this functionality?实现此功能的最佳方法是什么?

You can create a custom window and add dependency property to it您可以创建自定义窗口并向其添加依赖项属性

public class CustomWindow : Window
{
    // propdp FullScreenFromVm ...
}

Then you just need to change那么你只需要改变

<Window ... >

to

<local:CustomWindow ... FullScreen="{Binding FullScreenFromVm}">

and change base class of window并更改窗口的基类

public partial class MainWindow : CustomWindow
{
   ...
}

This way dependency property is available in xaml.这种方式依赖属性在 xaml 中可用。


Initially I thought you could just write <local:MainWindow ...> to get access to defined dependency property, but unfortunately this breaks code generation, because x:Class expects the containing type to be a base class.最初我认为你可以只编写<local:MainWindow ...>来访问定义的依赖属性,但不幸的是这会破坏代码生成,因为x:Class期望包含类型是类。

Provided that the MainWindow's DataContext is set to an object with a FullScreenFromVm property, this should work:假设 MainWindow 的DataContext设置为具有FullScreenFromVm属性的对象,这应该可以工作:

<Window x:Class="WindowDependencyPropertyTest.MainWindow" ...>
    <Window.Style>
        <Style>
            <Setter Property="local:MainWindow.FullScreen"
                    Value="{Binding FullScreenFromVm}"/>
        </Style>
    </Window.Style>
    ...
</Window>

Or you bind the property in the MainWindow constructor:或者您在 MainWindow 构造函数中绑定属性:

public MainWindow()
{
    InitializeComponent();
    SetBinding(FullScreenProperty, new Binding("FullScreenFromVm"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM