我对那些肯定是最常见的WPF要求之一感到困惑.我已经阅读了这个问题但是我的解决方案的实现不起作用.
这是无视控件的标记:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTest">
<Style TargetType="{x:Type local:CustomControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl}">
<Border>
<TextBox x:Name="myTextBox" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused"
Value="True">
<Setter Property="FocusManager.FocusedElement"
Value="{Binding ElementName=myTextBox}" />
<Setter TargetName="myTextBox"
Property="Background"
Value="Green" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
这是包含CustomControl实例的Window的标记:
<Window x:Class="WpfTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTest"
Title="Window1" Height="300" Width="300">
<local:CustomControl x:Name="CCtl" />
</Window>
Run Code Online (Sandbox Code Playgroud)
这是代码隐藏的代码:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Loaded += (RoutedEventHandler)delegate { CCtl.Focus(); };
}
}
Run Code Online (Sandbox Code Playgroud)
加载Window1时,文本框变为绿色(表示触发器有效)但焦点仍然是CCtl而不是文本框.毫无疑问,这与显示以下数据错误的输出有关:
无法找到引用'ElementName = myTextBox'的绑定源.BindingExpression …
我有一个显示文字的窗口.文本分为两部分:第一部分是固定的,第二部分需要是窗口上声明的DependencyProperty的内容.
我考虑使用包含两个Spans的TextBlock,其中第一个包含固定内容,第二个包含变量内容,但我在Span类中看不到任何明显允许我绑定到上述DependencyProperty的内容.
我目前正在使用并排堆叠的两个标签,但这很难看,如果我想检索整个文本块的内容,就无法帮助我(正如我在窗口太窄时显示工具提示时所做的那样)显示整个文本块).
任何人都可以帮我解决这个看似简单的问题吗?谢谢.
我想确保选定的ListViewItem的非聚焦背景与聚焦背景相同.我知道这样做的常用方法如下:
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue"/>
<SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Blue"/>
Run Code Online (Sandbox Code Playgroud)
但是,问题是我不想指定颜色,我只想要其键为ControlBrushKey的静态资源返回的Brush与HighlightBrushKey的刷子相同.
我有一个在 XAML 中声明的视图(见下文)。关联的视图模型是使用 MEF 自动创建的。我希望能够做这样的事情:
<local:MyControl Owner={x:Static local:Owners.ProjectOwner} />
Run Code Online (Sandbox Code Playgroud)
所需的净效果是将某些视图模型属性设置为等于 Owners.ProjectOwner。
我可以使用 hacky 代码隐藏来实现所需的结果,但宁愿通过绑定或一些类似的方式来做到这一点。任何人都可以建议一种方法吗?
更新
我让自己去写一个行为。但是,我没有为一个特定的案例付出所有的努力,而是将我的解决方案通用化,并将其包含在下面以防万一有人感兴趣。这是一种混合行为 (System.Windows.Interactivity.dll),但也很容易成为传统的附加行为。
using System;
using System.Windows;
using System.Windows.Interactivity;
namespace BlendBehaviors
{
public class SetViewModelPropertyBehavior : Behavior<FrameworkElement>
{
public static readonly DependencyProperty PropertyNameProperty =
DependencyProperty.Register("PropertyName", typeof(string), typeof(SetViewModelPropertyBehavior));
public static readonly DependencyProperty PropertyValueProperty =
DependencyProperty.Register("PropertyValue", typeof(object), typeof(SetViewModelPropertyBehavior));
public SetViewModelPropertyBehavior()
{ }
public string PropertyName
{
get { return (string)GetValue(PropertyNameProperty); }
set { SetValue(PropertyNameProperty, value); }
}
public object PropertyValue
{
get { return GetValue(PropertyValueProperty); }
set { …Run Code Online (Sandbox Code Playgroud) 我正在考虑实例方法Object.Equals(Object).使用反射,可以将此方法的IL作为字节数组获取,如下所示:
var mi = typeof(object).GetMethod("Equals", BindingFlags.Instance | BindingFlags.Public);
var mb = mi.GetMethodBody();
var bytes = mb.GetILAsByteArray();
Run Code Online (Sandbox Code Playgroud)
我有两台PC:一台是运行Windows XP的32位机器,另一台是Windows 7的64位机器.两台机器都安装了.NET Framework 4.0.30319 SP1Rel.
在x86机器上,生成的数组是:
[0]: 2
[1]: 3
[2]: 40
[3]: 122
[4]: 67
[5]: 0
[6]: 6
[7]: 42
Run Code Online (Sandbox Code Playgroud)
但是在x64机器上,我得到了这个:
[0]: 2
[1]: 3
[2]: 40
[3]: 123
[4]: 67
[5]: 0
[6]: 6
[7]: 42
Run Code Online (Sandbox Code Playgroud)
第四个字节是不同的.
现在我知道mscorlib在64位平台上有两种版本.然而,ILDASM揭示了这种方法的IL在风味之间和机器之间是相同的.在x64机器上,我在"任何CPU"和"x86"都定位了上面的代码,但结果是一样的.
所以我的问题是,任何人都可以解释这两台机器之间的差异吗?
UPDATE
这是Object.Equals(Object)的C#和IL:
public virtual bool Equals(object obj)
{
return RuntimeHelpers.Equals(this, obj);
}
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: call bool …Run Code Online (Sandbox Code Playgroud) wpf ×4
xaml ×2
.net ×1
background ×1
cil ×1
focus ×1
focusmanager ×1
html ×1
il ×1
inline ×1
listviewitem ×1
mef ×1
mscorlib ×1
mvvm ×1
properties ×1
reflection ×1
resources ×1
text ×1