Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF中指定相对的弹出窗口大小_C#_Wpf - Fatal编程技术网

C# 在WPF中指定相对的弹出窗口大小

C# 在WPF中指定相对的弹出窗口大小,c#,wpf,C#,Wpf,在WPF中,我有以下弹出窗口numpad。它以固定大小显示在main显示屏的中心 <Popup PlacementTarget="{Binding ElementName=mainDisplay}" Placement="Center" Name="numpadPop"> <Frame Width="300" Height="300" Name="numpadFrame"> </Frame> </Popup> 错误:(我确实定义了

在WPF中,我有以下
弹出窗口
numpad。它以固定大小显示在
main显示屏的中心

<Popup PlacementTarget="{Binding ElementName=mainDisplay}" Placement="Center" Name="numpadPop">
    <Frame Width="300" Height="300" Name="numpadFrame">
    </Frame>
</Popup>
错误:(我确实定义了MySizeConverter类)


更新:
最初,
MySizeConverter
不是
public
,这似乎是导致此问题的原因。

您可以将框架高度绑定到
main display
的高度,还需要指定一个接受
main display
高度并返回框架高度的

<Frame Height="{Binding ElementName=mainDisplay, Path=ActualHeight, Converter={StaticResource sizeConverter}}" Width="{Binding ElementName=mainDisplay, Path=ActualWidth, Converter={StaticResource sizeConverter}}" Name="numpadFrame">
</Frame>
将它添加到应用程序的资源字典(App.xaml),以便您可以使用
{StaticResource key}
引用它

<Application x:Class="yournamespace.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:yournamespace"
         StartupUri="MainWindow.xaml">
    <Application.Resources>
        <local:sizeConverter x:Key="sizeConverter" />
    </Application.Resources>
</Application>

如果您在编译代码和XAML时遇到问题。

我在这行上遇到了一个错误
。错误是,
只能在“IDictionary”中包含的元素上使用“Key”属性。
知道为什么吗?再次感谢你的帮助!你把这条线放在哪里?它必须在资源字典中,如
确保类
myizeconverter
的名称空间是
myappname
,而不是
myappname。是的,它只是
myappname
。谢谢你的帮助,当我发现错误时,我会发回的。
<Frame Height="{Binding ElementName=mainDisplay, Path=ActualHeight, Converter={StaticResource sizeConverter}}" Width="{Binding ElementName=mainDisplay, Path=ActualWidth, Converter={StaticResource sizeConverter}}" Name="numpadFrame">
</Frame>
public class sizeConverter : System.Windows.Data.IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return 0.8 * (double)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
<Application x:Class="yournamespace.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:yournamespace"
         StartupUri="MainWindow.xaml">
    <Application.Resources>
        <local:sizeConverter x:Key="sizeConverter" />
    </Application.Resources>
</Application>