对于喜欢WPF绑定挑战的人:
我有一个近乎功能的例子,双向绑定一个复选框到一个标志枚举的单个位(感谢Ian Oakes,原始的MSDN帖子).但问题是绑定的行为就好像它是一种方式(UI到DataContext,反之亦然).因此,复选框不会初始化,但如果切换,则数据源会正确更新.Attached是定义一些附加依赖项属性的类,以启用基于位的绑定.我注意到,即使我强制DataContext更改,也永远不会调用ValueChanged.
我尝试过的:更改属性定义的顺序,使用标签和文本框确认DataContext正在冒泡更新,任何合理的FrameworkMetadataPropertyOptions(AffectsRender,BindsTwoWayByDefault),显式设置绑定模式= TwoWay,敲打墙头,改变如果发生冲突,请将ValueProperty添加到EnumValueProperty.
任何建议或想法将非常感谢,感谢您提供的任何东西!
枚举:
[Flags]
public enum Department : byte
{
None = 0x00,
A = 0x01,
B = 0x02,
C = 0x04,
D = 0x08
} // end enum Department
Run Code Online (Sandbox Code Playgroud)
XAML用法:
CheckBox Name="studentIsInDeptACheckBox"
ctrl:CheckBoxFlagsBehaviour.Mask="{x:Static c:Department.A}"
ctrl:CheckBoxFlagsBehaviour.IsChecked="{Binding Path=IsChecked, RelativeSource={RelativeSource Self}}"
ctrl:CheckBoxFlagsBehaviour.Value="{Binding Department}"
Run Code Online (Sandbox Code Playgroud)
班级:
///
/// A helper class for providing bit-wise binding.
///
public class CheckBoxFlagsBehaviour
{
private static bool isValueChanging;
public static Enum GetMask(DependencyObject obj)
{
return (Enum)obj.GetValue(MaskProperty);
} // end GetMask
public static …
Run Code Online (Sandbox Code Playgroud) 我正在创建一个从切换按钮扩展的简单自定义控件,允许用户直接在XAML中指定已检查和未检查的内容.它运行良好,但它基于一个触发器,我不知道如何定义触发器,除了一个样式.如果我定义样式,那么我将失去在自定义控件之外设置的任何内容.
我希望能够做的只是将此触发器附加到控件上其他位置设置的任何现有样式.
这是样式/触发器的XAML.
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}" BasedOn="{StaticResource {x:Type ToggleButton}}">
<Setter Property="Content" Value="{Binding RelativeSource={RelativeSource Self}, Path=UncheckedContent}" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Content"
Value="{Binding RelativeSource={RelativeSource Self}, Path=CheckedContent}" />
</Trigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
Run Code Online (Sandbox Code Playgroud)
我尝试使用默认类型通过BasedOn继承样式,但如果自定义控件具有由其父级设置的显式样式,则它将无法工作.我也考虑过EventTriggers,但我不相信会有一个事件来初始化控件.
感谢任何人都能提供的帮助.:)
WPF TextBox
本身使用系统突出显示颜色来绘制所选文本的背景.我想覆盖它并使其保持一致,因为它因操作系统/用户主题而异.
对于ListBoxItem
s,有一个巧妙的技巧(见下文),您可以覆盖资源键以HighlightBrushKey
在焦点设置中自定义系统突出显示颜色:
<Style TargetType="ListBoxItem">
<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightGreen"/>
</Style.Resources>
</Style>
Run Code Online (Sandbox Code Playgroud)
TextBox
不幸的是,同样的技巧并不适用.有没有人有任何其他的想法,除了"覆盖ControlTemplate
"?
谢谢你的任何建议!
wpf ×3
c# ×2
styles ×2
data-binding ×1
enums ×1
highlighting ×1
resources ×1
textbox ×1
triggers ×1