我想在我的WPF中添加一个简单的(至少我认为是)行为TextBox
.
当用户按Escape时,我希望TextBox
他正在编辑以获得用户开始编辑时所具有的文本,并且我想从中移除焦点TextBox
.
我没有任何问题为编辑开头的值设置文本.
问题是要删除元素的焦点.我不想把焦点转移到任何其他组件,我只是想TextBox
失去焦点.我是否必须有一个不可见的元素来设置焦点,以便我TextBox
可以失去焦点?
我开始做一些网络开发,我正在学习表格并且碰到了这个领域:_lpchecked = "1"
.
我在StackOverflow问题中找到了带有此字段的表单示例. 大textinput使http post失败或在jQuery Formwizard插件的此示例页面中
我有一个典型的MVVM场景:我有一个ListBox绑定到一个StepsViewModel列表.我定义了一个DataTemplate,以便StepViewModel呈现为StepViews.StepView UserControl有一组标签和TextBox.
我想要做的是选择在关注textBox时包装StepView的ListBoxItem.我尝试使用以下触发器为我的TextBox创建一个样式:
<Trigger Property="IsFocused" Value="true">
<Setter TargetName="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Property="IsSelected" Value="True"/>
</Trigger>
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误,告诉我TextBox没有IsSelected属性.我现在,但Target是一个ListBoxItem.我怎样才能使它工作?
我正在创建一个HUD窗口,用于检查WinForms应用程序中的商业实体.
我希望有一个完全不同的窗口样式(最小化内容区域,只显示TitleBar,没有系统按钮等)所以我为此创建了一个WPF应用程序.
问题是我希望这个窗口在我的WinForms应用程序中"生存".我不能只是将WPF添加为OwnedForm或将主窗体设置为WPF窗口的所有者.
那么,怎么能实现呢?
编辑:感谢pst我找到了答案.这是片段:
System.Windows.Forms.Integration.ElementHost.EnableModelessKeyboardInterop(_inspector);
WindowInteropHelper inspectorHelper = new WindowInteropHelper(_inspector);
inspectorHelper.Owner = this.Handle;
_inspector.Show();
Run Code Online (Sandbox Code Playgroud) 我强调'任何'的原因是因为CanContentScroll
我的工作没有充分发挥作用ScollViewer
.让我解释一下这个场景:我有一个ScrollViewer
标签,后面跟ListBox
各个标签.我之所以有这个内容的原因ScrollViewer
是因为我不希望每个ListBox
人都有一个ScrollBar
,我只想要一个"全球" ScrollBar
.问题是,当光标在在ListBox
在ScrollViewer
不滚动.我试图CanContentScroll
在ScrollViewer,ListBox和ListBoxItem样式中将属性设置为true,但没有成功.我应该使用其他控件类型吗?这是我的代码示例:
<UserControl x:Class="Telbit.TeStudio.View.Controls.TestStepsView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:Telbit.TeStudio.View.Controls">
<UserControl.Resources>
<DataTemplate DataType="{x:Type my:TestStepsStepViewModel}">
<my:TestStepsStepView HorizontalAlignment="Stretch"/>
</DataTemplate>
<Style x:Key="StepItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
<Setter Property="ScrollViewer.CanContentScroll" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" SnapsToDevicePixels="true" Background="Transparent" BorderThickness="0" Padding="1">
<ContentPresenter/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter TargetName="Border" Property="Background" Value="#40a0f5ff"/>
</Trigger>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" …
Run Code Online (Sandbox Code Playgroud) 我有一个为我定义的样式ListBoxItems
,触发器设置IsSelected
为True 时的背景颜色:
<Style x:Key="StepItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="Border" Padding="0" SnapsToDevicePixels="true">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="#40a0f5ff"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
这种风格即使在ListBox
和ListBoxItem
失去焦点时也能保持所选项目,在我的情况下绝对是必须的.问题是我还希望ListBoxItem
在其中一个TextBox
孩子得到关注时被选中.为了达到这个目的,我添加了一个触发器,IsSelected
当IsKeyboardFocusWithin
为true 时设置为true:
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Setter Property="IsSelected" Value="True" />
</Trigger>
Run Code Online (Sandbox Code Playgroud)
当我添加此触发器时,焦点在子节点上时会选择Item TextBox
,但第一个行为会消失.现在当我点击外面时ListBox
,该项目被取消选中.
我怎样才能保持这两种行为?
我有一个TextBox,其Value绑定到ViewModel属性:
<TextBox Name="txtRunAfter" Grid.Column="4" Text="{Binding Mode=TwoWay, Path=RunAfter}" Style="{StaticResource TestStepTextBox}"/>
Run Code Online (Sandbox Code Playgroud)
set和get工作正常,直到我设置Value时添加一些验证:
private int _runAfter = 0;
public string RunAfter
{
get
{
return _runAfter.ToString();
}
set
{
int val = int.Parse(value);
if (_runAfter != val)
{
if (val < _order)
_runAfter = val;
else
{
_runAfter = 0;
OnPropertyChanged("RunAfter");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
虽然已达到OnPropertyChanged(我已经相同),但视图不会更改.我怎样才能做到这一点?
谢谢,JoséTavares
我有一个内置WinForms按钮的WPF窗口.当我设置窗口的样式时,按钮不会被渲染,但是当未设置窗口样式时,按钮将按原样显示.
窗口Xaml:
<Window x:Class="Telbit.TeStudio.View.Forms.FloatingTestComponentsBrowser"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="Test Components Browser" Style="{DynamicResource TSHUD}"
SizeToContent="WidthAndHeight" Closing="Window_Closing" >
<Grid Name="windowContent" Height="300" Width="300">
<WindowsFormsHost HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<wf:Button Text="Try" Name="btnTry" MaximumSize="100,25" BackColor="LightGray"/>
</WindowsFormsHost>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
这是窗口的样式:
<Style x:Key="TSHUD" TargetType="{x:Type Window}">
<Setter Property="ShowInTaskbar" Value="False"/>
<Setter Property="BorderThickness" Value="0px"/>
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid x:Name="LayoutRoot">
<Rectangle Fill="#ED111F29" Stroke="Black" Margin="29,29,29,29" RadiusX="3" RadiusY="3" Effect="{DynamicResource TSWindowShadow}"/>
<Rectangle Name="TSHUDHeader" Stroke="Black" Margin="29,29,29,29" VerticalAlignment="Top" Height="25" RadiusX="3" RadiusY="3">
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#ED1F3A45"/>
<GradientStop Color="#EC111F29" Offset="1"/> …
Run Code Online (Sandbox Code Playgroud) 我正在做一个Java Record/Replay工具,我需要从我的主Java应用程序启动Java应用程序.我需要访问EventDispatchThread以拦截事件并记录它们,所以我通过反射启动应用程序(简化了代码片段):
Class<?> app = Class.forName(mainClass);
Method m = app.getMethod("main", new Class[] { String[].class });
m.invoke(null, new Object[] { new String[] {} });
Run Code Online (Sandbox Code Playgroud)
我以前动态地将所有jar加载到类路径,并且应用程序几乎完美地启动.
当应用程序需要访问任何文件并使用相对路径执行时,会发生此问题.因为应用程序是通过我的应用程序启动的,所以路径与从其正常路径启动的路径不同,并且找不到文件.
我该怎么做才能解决这个问题?动态改变执行环境?有任何想法吗?
我正在尝试使用IDataErrorInfo验证我的MVVM应用程序中的数据,但我遇到了一些问题.
当我使用无效值设置我的TextBox时,验证工作正常.但是在我将TextBox的值设置为有效值后,我得到了以下异常:
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
System.Windows.Data Error: 16 : Cannot get 'Item[]' value (type 'ValidationError') from '(Validation.Errors)' (type 'ReadOnlyObservableCollection`1'). BindingExpression:Path=(0).[0].ErrorContent; DataItem='TextBox' (Name='txtRunAfter'); target element is 'TextBox' (Name='txtRunAfter'); target property is 'ToolTip' (type 'Object') TargetInvocationException:'System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter …
Run Code Online (Sandbox Code Playgroud) 我有一个ImageList
填充,你猜对了,图像.这些图像作为数据集加载到数据集的内存中Bitmap
.直到我把它们装进ImageList
内存的崛起并不担心.但是当它们被添加到ImageList
内存使用天空火箭时.但最大的问题是我必须重新加载图像列表.我试图在列表中的每个图像上调用dispose但是内存没有被释放.这是我试图清理内存的代码:
foreach (Image item in imageList.Images)
{
item.Dispose();
}
imageList.Images.Clear();
GC.Collect();
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
wpf ×8
binding ×3
mvvm ×3
textbox ×3
listbox ×2
listboxitem ×2
winforms ×2
focus ×1
forms ×1
html ×1
imagelist ×1
java ×1
javascript ×1
reflection ×1
scrollviewer ×1
styles ×1
validation ×1