我试图显示一个ComboBox,其ItemsSource是一个控件的集合(它是一个PropertyGrid的一部分,ComboBox应该显示控件的名称,用户应该能够选择其中一个控件).这是一个非常简化的问题再现:
<ComboBox ItemsSource="{Binding GroupBoxes}" SelectedValue="{Binding SelectedGroupBox}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
GroupBoxes和SelectedGroupBox是ObservableCollection和GroupBox类型的DependencyProperties.
Bindings工作 - 控件名称显示在ComboBox-DropDown中,如果我选择不同的项目,我可以看到SelectedGroupBox属性已正确更新.问题:所选项目永远不会显示在ComboBox中.从代码设置SelectedGroupBox属性也可以按预期工作 - ComboBox引发SelectionChanged并且其SelectedValue是正确的,但它仍然不显示当前值.
如果我对任何其他类型的类做同样的事情,一切都按预期工作.
在寻找答案时,我遇到了许多有类似声音问题的人的帖子,但几乎所有这些都是绑定问题,而这并非如此.
编辑:
为了简化尝试,这里是代码背后的代码.只需将上面的XAML放在一个新窗口中,然后将代码放在后面的代码中.
public MainWindow() {
InitializeComponent();
this.DataContext = this;
this.GroupBoxes = new ObservableCollection<GroupBox>();
this.GroupBoxes.Add(new GroupBox() { Name = "AAA", Header = "AAA", Height = 100, Background = Brushes.Purple });
this.GroupBoxes.Add(new GroupBox() { Name = "BBB", Header = "BBB", Height = 100, Background = Brushes.Purple });
this.GroupBoxes.Add(new GroupBox() { Name = "CCC", Header = "CCC", Height = 100, Background …Run Code Online (Sandbox Code Playgroud) 在某些情况下,我非常喜欢静态事件,但事实上我很少在其他人的代码中看到它们,这让我想知道我是否遗漏了一些重要的东西.我在这个网站上发现了很多关于静态事件的讨论,但是大多数都讨论了我不感兴趣的情况(比如静态类)或者我想不首先使用它们的情况.
我很感兴趣的是,我可能有东西,并长期居住"经理"对象,它反应在这些情况下的东西的一个实例很多情况下的情况.一个非常简单的例子来说明我的意思:
public class God {
//the list of followers is really big and changes all the time,
//it seems like a waste of time to
//register/unregister events for each and every one...
readonly List<Believer> Believers = new List<Believer>();
God() {
//...so instead let's have a static event and listen to that
Believer.Prayed += this.Believer_Prayed;
}
void Believer_Prayed(Believer believer, string prayer) {
//whatever
}
}
public class Believer {
public static event Action<Believer, string> Prayed;
void Pray() { …Run Code Online (Sandbox Code Playgroud) 我正在尝试不同的策略,用于从控件的左边缘到右边缘绘制图形.到目前为止,我们使用的是带有折线的Canvas,它可以正常运行,但仍然可以使用一些改进.
当我尝试使用DrawingContext.DrawLine时,我遇到了令人难以置信的糟糕表现,我无法弄清楚原因.这是我能提出的最简洁的代码,它演示了这个问题:
public class TestControl : Control {
static Pen pen = new Pen(Brushes.Gray, 1.0);
static Random rnd = new Random();
protected override void OnRender(DrawingContext drawingContext) {
var previousPoint = new Point(0, 0);
for (int x = 4; x < this.ActualWidth; x += 4) {
var newPoint = new Point(x, rnd.Next((int)this.ActualHeight));
drawingContext.DrawLine(pen, previousPoint, newPoint);
previousPoint = newPoint;
}
}
}
Run Code Online (Sandbox Code Playgroud)
而MainWindow.xaml只包含这个:
<StackPanel>
<l:TestControl Height="16"/>
<!-- copy+paste the above line a few times -->
</StackPanel>
Run Code Online (Sandbox Code Playgroud)
现在调整窗口大小:根据StackPanel中TestControls的数量,我会遇到明显的延迟(10个控件)或30秒完全停顿(100个控件),我甚至无法点击"停止调试器"-Button在VS ...
我对此很困惑,显然我做错了但是由于代码很简单,我看不出那可能是什么......我正在使用.Net4以防万一.
我正在寻找一种为Segoe MDL2图标添加颜色的方法。我的应用程序中的字形当前是TextBlock资源,其定义如下:
<TextBlock x:Key="PenSymbol" x:Shared="False" FontFamily="Segoe MDL2 Assets" Text="" FontSize="16" TextOptions.TextRenderingMode="Grayscale"/>
Run Code Online (Sandbox Code Playgroud)
这是Window 10 Sketchpad中工具栏的屏幕截图(应用了Creators更新之后)。
编辑:我知道如何更改文本颜色,我试图弄清楚如何获得“部分填充”效果(屏幕截图中的蓝色,黑色和黄色)。
Edit2:必须显示2个不同的字形才能实现此效果。在最后一次更新中,背景所需的字形已添加到Segoe MDL2字体中。感谢mm8向我指出正确的方向。
XAML:
<Style x:Key="SymbolText" TargetType="{x:Type TextBlock}">
<Setter Property="FontSize" Value="16"/>
<Setter Property="UseLayoutRounding" Value="True"/>
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="FontFamily" Value="Segoe MDL2 Assets"/>
<Setter Property="TextOptions.TextRenderingMode" Value="Grayscale"/>
</Style>
<StackPanel Orientation="Horizontal">
<Grid Margin="4">
<TextBlock Text="" Style="{StaticResource SymbolText}" Foreground="OrangeRed"/>
<TextBlock Text="" Style="{StaticResource SymbolText}"/>
</Grid>
<Grid Margin="4">
<TextBlock Text="" Style="{StaticResource SymbolText}" Foreground="LightGreen"/>
<TextBlock Text="" Style="{StaticResource SymbolText}"/>
</Grid>
<Grid Margin="4">
<TextBlock Text="" Style="{StaticResource SymbolText}" Foreground="LightBlue"/>
<TextBlock Text="" Style="{StaticResource …Run Code Online (Sandbox Code Playgroud)