C# 在WPF中指定相对的弹出窗口大小
在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> 错误:(我确实定义了
弹出窗口
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>