我有一个用户控件,我MainWindow在运行时加载.我无法从包含窗口获取句柄UserControl.
我试过了this.Parent,但它总是空的.有谁知道如何从WPF中的用户控件获取包含窗口的句柄?
以下是控件的加载方式:
private void XMLLogViewer_MenuItem_Click(object sender, RoutedEventArgs e)
{
MenuItem application = sender as MenuItem;
string parameter = application.CommandParameter as string;
string controlName = parameter;
if (uxPanel.Children.Count == 0)
{
System.Runtime.Remoting.ObjectHandle instance = Activator.CreateInstance(Assembly.GetExecutingAssembly().FullName, controlName);
UserControl control = instance.Unwrap() as UserControl;
this.LoadControl(control);
}
}
private void LoadControl(UserControl control)
{
if (uxPanel.Children.Count > 0)
{
foreach (UIElement ctrl in uxPanel.Children)
{
if (ctrl.GetType() != control.GetType())
{
this.SetControl(control);
}
}
}
else
{
this.SetControl(control);
} …Run Code Online (Sandbox Code Playgroud) 默认情况下,Validation.ErrorTemplate在WPF只是一个小红色边框没有任何ToolTip.
在Silverlight 4中,验证错误很好地开箱即用.
以下是Silverlight 4和WPF中出现的验证错误的比较
Silverlight 4

WPF

请注意WPF版本的平坦,无聊的外观与我认为的Silverlight外观相比.
WPF框架中是否存在任何类似的验证样式/模板,或者是否有人创建了很好的样式验证模板,如上面的Silverlight版本?或者我是否必须从头开始创建它们?
如果有人想尝试一下,可以使用以下代码重现上面的验证错误,适用于Silverlight和WPF
主窗口/ MainPage.xaml中
<StackPanel Orientation="Horizontal" Margin="10" VerticalAlignment="Top">
<TextBox Text="{Binding Path=TextProperty, Mode=TwoWay, ValidatesOnExceptions=True}"/>
<Button Content="Tab To Me..." Margin="20,0,0,0"/>
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
主窗口/ MainPage.xaml.cs中
public MainWindow/MainPage()
{
InitializeComponent();
this.DataContext = this;
}
private string _textProperty;
public string TextProperty
{
get { return _textProperty; }
set
{
if (value.Length > 5)
{
throw new Exception("Too many characters");
}
_textProperty = value;
} …Run Code Online (Sandbox Code Playgroud) 我正在编写一个带有复杂Popup的XBAP(Canvas Z-index为99,其上有一个网格...),我想"附加"到打开它的按钮,并按下该按钮,无论它在哪里进行屏幕.例如,如果按钮位于ListBox或XamDataGrid中,我希望弹出按钮在滚动时跟随按钮.如果它在扩展器下方,我希望它在扩展器强制移动时保持与按钮的连接等.
有任何想法吗?
当我点击它时,我想在切换按钮下放置一个弹出窗口。在这个弹出窗口中,我想添加按钮和其他控件。但是,当我调整我的 mian 窗口大小时,如何确保弹出窗口始终位于我的切换按钮下方。
我的 XAML 代码:
<ToggleButton
x:Name="userBtn"
Margin="610,25,378,0"
VerticalAlignment="Top"
Height="29"
Grid.Column="2"
>
<ToggleButton.Resources>
<BitmapImage x:Key="imgNormal" UriSource="/Resources/home.jpg"/>
<BitmapImage x:Key="imgHover" UriSource="/Resources/home_checked.jpg"/>
<BitmapImage x:Key="imgChecked" UriSource="/Resources/home_checked.jpg"/>
</ToggleButton.Resources>
<ToggleButton.Style>
<Style TargetType="ToggleButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton" >
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Width="150" Height="32">
<Image x:Name="PART_Image" Height="22" Width="22" RenderOptions.BitmapScalingMode="HighQuality" HorizontalAlignment="Center" Margin="0,0,0,0" Source="{StaticResource imgNormal}"/>
<TextBlock x:Name="PART_TEXT" FontFamily="Arial" Foreground="#FFAFADAD" FontSize="13" HorizontalAlignment="Center" Text="{x:Static properties:Resources.BtnDashboard}" Margin="10,9,0,0"></TextBlock>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter TargetName="PART_Image" Property="Source" Value="{StaticResource imgChecked}"/>
<Setter TargetName="PART_TEXT" Property="Foreground" Value="#79B539" />
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter TargetName="PART_Image" Property="Source" Value="{StaticResource imgHover}"/> …Run Code Online (Sandbox Code Playgroud) wpf ×4
c# ×3
popup ×2
.net ×1
position ×1
screen ×1
silverlight ×1
styles ×1
togglebutton ×1
xbap ×1