Wpf 使用当前资源

在xaml中不能在资源指定前使用

只能在申明顺序之后使用,并且是使用 Property Element 语法
使用Binding代替StaticResource
Path="."表示当前对象(Source指向的那个)

<Binding Mode="OneTime" Path="." Source="{StaticResource HeightKey}" />

完整代码

<Window
    x:Class="TrykleWpfTestApp4.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:local="clr-namespace:TrykleWpfTestApp4"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    Title="MainWindow"
    mc:Ignorable="d">

    <Window.Resources>
        <sys:Double x:Key="HeightKey">200</sys:Double>
    </Window.Resources>

    <Window.Width>
        <sys:Double>400</sys:Double>
    </Window.Width>

    <Window.Height>
        <Binding Mode="OneTime" Path="." Source="{StaticResource HeightKey}" />
    </Window.Height>

    <Grid />
</Window>

posted @ 2023-02-04 17:11  trykle  阅读(55)  评论(0)    收藏  举报