小编DHN*_*DHN的帖子

从值转换器访问资源字典中的颜色

我在ResourceDictionary中定义了几种颜色.例如:

<ResourceDictionary ...>
  <Color x:Key=Gray1>#FFF7F1F3</Color>
  <Color x:Key=Gray2>#FFDDD8DA</Color>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

所以我可以在应用程序的各处重用它们.

现在我写了一个值转换器来将项目内部状态转换为相关颜色.

如何在值转换器的代码中访问定义的颜色?

我的第一个想法是将字典作为转换器参数传递.但我不知道如何实现这一目标.



问候

编辑

Application.Current.Resources不是一种选择.因为我以后无法访问它.

c# silverlight wpf

17
推荐指数
1
解决办法
7338
查看次数

有没有最佳实践方法来验证用户输入?

有没有最佳实践方法来验证用户输入

实际问题:

用户在窗口中给出某些输入.当他完成这些输入后,他可以点击"创建".现在,应显示一条弹出消息,其中包含所有无效输入.如果没有无效输入,那么继续.

我可以在Form类中轻松完成此操作.但我记得在设置属性中验证输入的一些最佳实践方法.问题是我已经创建了该类的实例(否则,无法设置属性;))如果我以这种方式验证.这不应该发生,除非输入有效,否则不会创建类的实例.

我打算创建一个ErrorMessages类,其中包含一个列表,我可以将所有errorMessages放入其中.每次给出无效输入时,都会向errorMessages列表添加一条新消息.因此,如果用户单击"创建"按钮,则会显示列表中的所有消息.这是处理事情的好方法吗?

那么有最好的练习方式吗?任何提供此类解决方案的设计模式?

编辑:这是一项学校任务.因此有不合逻辑的要求.单击"创建"时,我必须显示所有无效输入.我想从Form类中做到这一点.(因此即使没有GUI,验证也能正常工作,此时我甚至还没有创建GUI).首先确保我的功能正常工作;).我想保持我的代码干净,抽象和OOP.那么我应该如何显示我的错误消息?

c# input winforms

10
推荐指数
1
解决办法
1万
查看次数

我为什么要避免使用Dispatcher?

我已经阅读了很多关于绑定和GUI控件的线程亲和性的帖子,文章等.有些帖子中人们不想使用Dispatcher.

我还有一个同事避免在他的代码中使用Dispatcher.我问他原因,但他的回答并不能让我满意.他说,他不喜欢隐藏在课堂上的这种"魔法".

好吧,我是下一堂课的粉丝.


public class BindingBase : INotifyPropertyChanged
{
   public event PropertyChangedEventHandler PropertyChanged;

   private Dispatcher Dispatcher
   {
#if SILVERLIGHT
      get { return Deployment.Current.Dispatcher; }
#else
      get { return Application.Current.Dispatcher; }
#endif
   }

   protected void RaisePropertyChanged<T>(Expression<Func<T>> expr)
   {
      var memberExpr = (MemberExpression)expr.Body;
      string property = memberExpr.Member.Name;

      var propertyChanged = PropertyChanged;
      if (propertyChanged == null) return;

      if (Dispatcher.CheckAccess())
         propertyChanged.Invoke(this, new PropertyChangedEventArgs(property));
      else
         Dispatcher.BeginInvoke(() => RaisePropertyChanged(expr));
   }
}
Run Code Online (Sandbox Code Playgroud)

这是个问题.有些人不想使用这样的课程有什么理由吗?也许我必须重新考虑这种方法.

你必须承认,有一件奇怪的事情.Dispatcher.CheckAccess()被Intellisense排除在外.由于这个事实,也许他们有点可怕.

问候

编辑:

好的,另一个例子.考虑一个复杂的对象.作为例子的集合可能不是最好的主意.


public class ExampleVm : BindingBase
{ …
Run Code Online (Sandbox Code Playgroud)

data-binding silverlight wpf

7
推荐指数
1
解决办法
1773
查看次数

用于从MDX查询中提取元素的正则表达式

我想从给定的MDX查询中提取信息或查询轴元素.假设我有这个查询:

SELECT NON EMPTY {
    Hierarchize({[Product].[Product Family].Members})
} ON COLUMNS, 
NON EMPTY Hierarchize ( 
    Union ( 
        CrossJoin ( {[Time].[1997].[Q1]}, 
            CrossJoin ([Store].[Store Name].Members, 
                [Store Type].[Store Type].Members
            )
        ), 
        CrossJoin({[Time].[1997].[Q2]}, 
            CrossJoin([Store].[Store Name].Members, 
                [Store Type].[Store Type].Members
            )
        )
    )
) ON ROWS FROM [Sales] 
WHERE {
    Hierarchize({[Measures].[Unit Sales]})
}
Run Code Online (Sandbox Code Playgroud)

我想提取的子字符串有这样的模式:[...](.[...])或[...].[...].成员

请注意,...表示任何字母数字,并且括号内的含义可以重现.因此,我期望的结果是:

[产品].[产品系列].会员; [时间] [1997] [Q1]..[时间] [1997] [Q2]..[商店].[商店名称].会员; [商店类型].[商店类型].会员; [措施].[单位销售]

我尽我所能,最后想出了这个正则表达式:

\[.*?[A-Za-z\s]\](.*?(\.\[.*?[A-Za-z\s]\])|(\.Members))
Run Code Online (Sandbox Code Playgroud)

但结果是:

[产品].[产品系列]; [时间].[1997].[Q1]},CrossJoin([Store]; [Store Name] .Members,[Store Type].[Store Type]; [Time].[1997].[Q2]},CrossJoin ([Store]; [Store Name] .Members,[Store Type].[Store Type]; [Sales] WHERE {Hierarchize({[Measures].[Unit Sales]

有人可以对我的正则表达式进行任何更正吗?任何帮助,将不胜感激.

regex mdx

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

C#:使用类泛型参数覆盖泛型方法

为什么这不可能?

abstract class A
{
    public abstract T f<T>();
}

class B<T> : A
{
    public override T f()
    {
        return default (T);
    }
}
Run Code Online (Sandbox Code Playgroud)

错误:

does not implement inherited abstract member 'A.f<T>()'
no suitable method found to override
Run Code Online (Sandbox Code Playgroud)

我知道签名必须是相同的,但从我的角度来看,我认为没有理由这是不允许的.另外我知道另一个解决方案是制作A通用的,而不是它的方法,但由于某些原因它不适合我.

c# generics

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

ThreadStatic和同步

我有以下代码.这可能是一个愚蠢的问题,但我不确定,如果同步是否必要.


class MyClass
{
  [ThreadStatic]
  private static object _staticObject;
  private static readonly LockStaticField = new object();

  public static object StaticObject
  {
     get
     {
        lock(LockStaticField)
        {
           return _staticObject ?? (_staticObject = new object());
        }
     }
  }
}
Run Code Online (Sandbox Code Playgroud)

我知道ThreadStatic字段不需要任何同步,因为状态不是共享的.但是静态getter和初始化是什么呢?

c# multithreading

3
推荐指数
1
解决办法
1242
查看次数

通过使用LINQ跳过空值,将Dictionary <int,int?>转换为Dictionary <int,int>

我有以下Product课程:

public class Product
{
    public string Name { get; set; }
    public float Price { get; set; }     
    public int? CategoryId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

现在我必须计算Product每个人有多少CategoryId并将它们放入一个Dictionary<int, int>.因此:

IQueryable<Product> products = _productServices.GetAll(); //return IQueryable<Product>

Dictionary<int, int> productDict =  products.ToList()
                                            .GroupBy(p => p.CategoryId)
                                            .ToDictionary(pgroup => pgroup.key, pgroup => pgroup.Count());
Run Code Online (Sandbox Code Playgroud)

问题是,我得到一个Dictionary<int?, int>ToDictionary().即使我通过放置预过滤空值Where(p => p.CategoryId != null)我不改变CategoryIdto 的类型int.我还尝试创建和匿名类型:

products.ToList()
        .GroupBy(p => p.CategoryId)
        .Select(p => …
Run Code Online (Sandbox Code Playgroud)

c# linq nullable

3
推荐指数
3
解决办法
1501
查看次数

自定义 WPF ComboBox 不显示分组

我在 WPF 中创建了一个自定义组合框

<!-- Combobox template -->
    <ControlTemplate x:Key="CustomComboBoxToggleButton" TargetType="ToggleButton">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="20"/>
            </Grid.ColumnDefinitions>
            <Border x:Name="Border" Grid.ColumnSpan="2" CornerRadius="3" Background="{StaticResource CustomDarkBlue}" BorderBrush="{StaticResource CustomWhite}" BorderThickness="2"/>
            <Border x:Name="Border2" Grid.Column="0" CornerRadius="3,0,0,3" Margin="1" Background="{StaticResource CustomBackground}" BorderBrush="{StaticResource CustomWhite}" BorderThickness="1,1,2,1"/>
            <Path x:Name="Arrow" Grid.Column="1" Fill="{StaticResource CustomGlyph}" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z"/>
        </Grid>
        <ControlTemplate.Triggers>
            <Trigger Property="ToggleButton.IsMouseOver" Value="True">
                <Setter TargetName="Border" Property="Background" Value="{StaticResource CustomDarkBlue}"/>
            </Trigger>
            <Trigger Property="ToggleButton.IsChecked" Value="True">
                <Setter TargetName="Border" Property="Background" Value="{StaticResource CustomDarkBlue}"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="False">                    
                <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource CustomGray}"/> …
Run Code Online (Sandbox Code Playgroud)

wpf xaml grouping combobox

2
推荐指数
1
解决办法
1944
查看次数

是否可以在变量名的名称中使用变量号?

(使用WPF)是否可以使用a的数量TextBox来选择参数名称?例如,用户可以将0,1,2或3作为文本放在TextBox:

private void button1_Click(object sender, RoutedEventArgs e)
{
    int test = int.Parse(textBox1.Text);

    string nr1 = "this string is 1";
    string nr2 = "this string is 2";
    string nr3 = "this string is 3";

    MessageBox.Show();
}
Run Code Online (Sandbox Code Playgroud)

现在MessageBox.Show();想要放东西,因为nr(test)
这样的事情可能吗?

c# variables wpf

2
推荐指数
1
解决办法
118
查看次数

如何在MVVM中实现INotifyCollectionChanged,以进行NotifyCollectionChangedAction的重置,添加,删除,移动,替换

我的ViewModelBase类是:

public abstract class ViewModelBase:INotifyPropertyChanged, IDisposable, INotifyCollectionChanged
{
    #region Constructor

    protected ViewModelBase()
    {
    }

    #endregion // Constructor
    #region DisplayName

    /// <summary>
    /// Returns the user-friendly name of this object. 
    /// Child classes can set this property to a new value,
    /// or override it to determine the value on-demand.
    /// </summary>
    public virtual string DisplayName { get; protected set; }

    #endregion // DisplayName


    #region Debugging Aides

    /// <summary>
    /// Warns the developer if this object does not have
    /// a …
Run Code Online (Sandbox Code Playgroud)

silverlight wpf xaml observablecollection mvvm

2
推荐指数
1
解决办法
6167
查看次数