小编Suk*_*ram的帖子

WPF ComboBox:显示错误的项目

这是最初的情况:

XAML:

<ComboBox Grid.Row="0"
           Grid.Column="1"
           Margin="0,3"
           HorizontalAlignment="Stretch"
           DisplayMemberPath="DisplayText"
           ItemsSource="{Binding ObjectSource}" />
Run Code Online (Sandbox Code Playgroud)

视图模型:

public Collection<MyObjects> ObjectSource
{
    get
    {
        return this.objectSource;
    }

    set
    {
        this.SetProperty(ref this.objectSource, value);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的对象包含一个名称(字符串),有效来自(dateTime)和一个displayText(字符串只有get),它结合了名称和有效来显示.

在这种简单的情况下,我可以打开组合框并查看所有条目,选择一个后它也会在组合框内显示正确的显示文本.现在我再次打开下拉区域并选择其他条目.结果是所选择的项目被切换(当再次打开下拉条目时,您可以看到高亮度项目).但是组合框内显示的项目没有改变,仍然有第一个选择的DisplayText.

结果情况的屏幕截图

有没有人知道为什么组合框不会更新?提前致谢

编辑:谢谢大家的帮助.问题是Equals的错误覆盖.

c# wpf combobox mvvm

6
推荐指数
1
解决办法
644
查看次数

在UserControl中设置ForegroundColor

我在WPF中编写用户控件,这是我自己的第一个控件.对于您的信息,我使用Telerik控件.

我的用户控件Grid只包含2 GridView秒.而现在我想GridView通过设置前景和背景给某人设置样式的可能性.

我都是这样设定的:

Background="{Binding ElementName=Grid, Path=DarkBackground}"
Foreground="{Binding ElementName=Grid, Path=LightForeground}"
Run Code Online (Sandbox Code Playgroud)

我的代码背后是:

public static DependencyProperty LightForegroundProperty = DependencyProperty.Register( "LightForeground", typeof( Brush ), typeof( ParameterGrid ) );
public Brush LightForeground
{
  get
  {
    return (Brush)GetValue( LightForegroundProperty );
  }
  set
  {
    SetValue( LightForegroundProperty, value );
  }
}

public Brush DarkBackground
{
  get
  {
    return (Brush)GetValue( DarkBackgroundProperty );
  }
  set
  {
    SetValue( DarkBackgroundProperty, value );
  }
}
Run Code Online (Sandbox Code Playgroud)

问题是我的前景,背景值在运行时被忽略.要为Foreground设置修复值,会带来预期结果.

我没有发现我的错误,有谁有想法?

c# wpf user-controls foreground wpf-controls

4
推荐指数
1
解决办法
2389
查看次数

标签 统计

c# ×2

wpf ×2

combobox ×1

foreground ×1

mvvm ×1

user-controls ×1

wpf-controls ×1