English 中文(简体)
WPF DataBled to Brand (in a PropertyGrid)
原标题:WPF DataBinding to Flag Enum (in a PropertyGrid)
  • 时间:2009-09-24 11:42:05
  •  标签:

我们需要有能力从世界森林论坛的角度来看(无论在哪里,在财产审查中)挑选出多种价值,就像旗帜列举的性质一样。

所涉财产为dynamic,任何事先界定的数据模板均不得用作“型号”在运行时间上发现。 (一) 能够发现点数是旗帜的数据模板可能证明是有用的,但根据我的理解,我需要提前了解国旗的Enum类型,以实现这一目标,而情况并非如此)。

我曾尝试过一些专有和开放源房地产网,似乎没有人支持。 旗帜在盒子外划出大体类型。

解决这个问题的办法,是使我能够把数据整理起来,为任何商业或公开来源的WPFGrid,为上述旗帜Enum选择多种价值。

<><>Code>:

例假财产 类型:

public class PropertyTypeOne
{
    public PropertyTypeOne()
    {
        IntProp = 1;
        InProp2 = 2;
        BoolProp = true;
        Boolprop2 = false;
        StringProp = "string1";
        DoubleProp = 2.3;
        EnumProp = FlagEnumDataTYpe.MarketDepth;
    }

    public int IntProp { get; set; }

    public int InProp2 { get; set; }

    public bool BoolProp { get; set; }

    public bool BoolProp2 { get; set; }

    public string StringProp { get; set; }

    public double DoubleProp { get; set; }

    //This is the property in question
    public FlagEnumDataType EnumProp { get; set; }
}

例举 类型:

[Flags]
public enum FlagEnumDataType : byte
{
    None = 0,
    Trade = 1,
    Quote = 2,
    MarketDepth = 4,
    All = 255
}

<>说明:

如果解决办法使用公开来源WPF PropertyGrid(。 http://www.codeplex.com/wpg。 我将实施控制方面的变动/补充。

www.un.org/Depts/DGACM/index_spanish.htm 感谢

最佳回答

我发现,这样做确实是明智的,但从与棉兰景的开发商交谈到这里,有些是粗略的,但却是功能性的,与棉兰吉德房地产公司合作。

第一,我们为船旗国编辑本身树立了一个模板。 这是使用WPF 财产审查图书馆EnumValuesConverter编制的物品:

<ms:EnumValuesConverter x:Key="evc" />
<local:FlaggyConverter x:Key="fc" />

<DataTemplate x:Key="FlagEditorTemplate">
  <ItemsControl Name="ic" ItemsSource="{Binding Value, Converter={StaticResource evc}}">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <CheckBox Content="{Binding}">
        </CheckBox>
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</DataTemplate>

现在,我们需要展示根据国旗是否在悬挂或卸下而检查的检查箱。 这就需要两点:第一,一台IMultiValueConverter,使其能够既考虑手头旗,又考虑背景价值;第二,个人检查箱可以读到背景价值。 (背景价值一指实际财产价值。) 例如,背景价值可能是旗帜1、旗帜4、旗帜32。 这里指转手:

public class FlaggyConverter : IMultiValueConverter
{
  public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    int flagValue = (int)values[0];
    int propertyValue = (int)values[1];

    return (flagValue & propertyValue) == flagValue;
  }

  public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
  {
    throw new NotImplementedException();
  }
}

为了宣传环境价值,我会采取捷径。 你可能更喜欢用更有意义的名称创建附属财产。

现在,控制将显示对悬挂的国旗的检查,但当你点击一个检查箱或外面时,则获得更新价值。 不幸的是,我找到的唯一方式是处理检查和检查事件,并逐手确定背景价值。 为此,我们需要把背景价值放在能够从检查箱活动操作员那里更新的地方。 这意味着双向将检查箱的财产与背景价值挂钩。 我再次使用塔格语,尽管你可能想做一点比较干净的东西;我也想利用直接事件处理,尽管你可能想把此事归结为一种附属行为(如果你在创造附带财产以保持环境价值,那将特别有益)。

<DataTemplate x:Key="FlagEditorTemplate">
  <ItemsControl Name="ic" ItemsSource="{Binding Value, Converter={StaticResource evc}}" Tag="{Binding Value, Mode=TwoWay}">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <CheckBox Content="{Binding}" Tag="{Binding Tag, ElementName=ic, Mode=TwoWay}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked">
          <CheckBox.IsChecked>
            <MultiBinding Converter="{StaticResource fc}" Mode="OneWay">
              <Binding />
              <Binding Path="Tag" ElementName="ic" />
            </MultiBinding>
          </CheckBox.IsChecked>
        </CheckBox>
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</DataTemplate>

请注意塔格的双向约束力:这样,当我们从我们的活动处理守则中划出“塔”时,它就会重新传播到原文。 从那里到财产价值。

活动处理者大多是显而易见的,但有一刀切:

<DataTemplate x:Key="FlagEditorTemplate">
  <ItemsControl Name="ic" ItemsSource="{Binding Value, Converter={StaticResource evc}}" Tag="{Binding Value, Mode=TwoWay}">
    <ItemsControl.ItemTemplate>
      <DataTemplate>
        <CheckBox Content="{Binding}" Tag="{Binding Tag, ElementName=ic, Mode=TwoWay}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked">
          <CheckBox.IsChecked>
            <MultiBinding Converter="{StaticResource fc}" Mode="OneWay">
              <Binding />
              <Binding Path="Tag" ElementName="ic" />
            </MultiBinding>
          </CheckBox.IsChecked>
        </CheckBox>
      </DataTemplate>
    </ItemsControl.ItemTemplate>
  </ItemsControl>
</DataTemplate>

活动管理员:

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
  CheckBox cb = (CheckBox)sender;
  int val = (int)(cb.Tag);
  int flag = (int)(cb.Content);
  val = val | flag;
  cb.Tag = (Curses)val;
}

private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
  CheckBox cb = (CheckBox)sender;
  int val = (int)(cb.Tag);
  int flag = (int)(cb.Content);
  val = val & ~flag;
  cb.Tag = (Curses)val;
}

注 页: 1 否则,世界森林论坛在试图将价值重新传播给来源时,就未能将价值转换为大类。 这里是我的头等。 如果你想要一个完全灵活的、类型的编辑,你就希望从外部提供这种财产,例如作为检查箱的附属财产。 你们要么利用皈依者,要么从编辑EditContext传播。

最后,我们需要把这一点看上去。 你们可以在财产基础上这样做:

<ms:PropertyGrid>
  <ms:PropertyGrid.Editors>
    <ms:PropertyEditor PropertyName="Curses" EditorTemplate="{StaticResource FlagEditorTemplate}" />
  </ms:PropertyGrid.Editors>
</ms:PropertyGrid>

或者通过使用聪明编辑声明,ook住所有类型有旗帜的不动产。 关于创建和使用智能编辑的资料,见

如果你想节省空间,你可以把“Control”项目改为“ComboBox”,尽管你需要做一些额外工作来处理倒塌的展示;我没有详细探讨这一问题。

问题回答

在互联网上创建,情况略有改善,但还没有时间进行测试。

/// <summary>
/// Two-way conversion from flags to bool and back using parameter as mask
/// Warning: The trick is in storing value locally between calls to Convert and ConvertBack
/// You must have a single instance of this converter per flags property per object
/// Do not share this converter between different objects or properties
/// Typical usage:
/// [Flags] enum FlagType { None = 0, Trade = 1, Quote = 2, Report = 4, All = 255 }
/// <local:EditableFlagsToBooleanConverter x:Key="FlagsToBooleanConverter" />
/// <CheckBox IsChecked="{Binding Prop1, Converter={StaticResource FlagsToBooleanConverter}, Mode=TwoWay, 
///     ConverterParameter={x:Static local:FlagType.Trade}}" >Trade</CheckBox>
/// <CheckBox IsChecked="{Binding Prop1, Converter={StaticResource FlagsToBooleanConverter}, Mode=TwoWay, 
///     ConverterParameter={x:Static local:FlagType.Quote}}" >Quote</CheckBox>
/// <CheckBox IsChecked="{Binding Prop1, Converter={StaticResource FlagsToBooleanConverter}, Mode=TwoWay, 
///     ConverterParameter={x:Static local:FlagType.Report}}" >Report</CheckBox>
/// </summary>
public class EditableFlagsToBooleanConverter : IValueConverter
{
    private ulong _target;

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (parameter is Enum && value is Enum)
        {
            var mask = (ulong) parameter;
            _target = (ulong) value;
            return ((mask & _target) != 0);
        }

        return Binding.DoNothing;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is bool && parameter is Enum)
        {
            var mask = (ulong)parameter;
            if ((bool)value)
            {
                _target |= mask;
            }
            else
            {
                _target &= ~mask;
            }
            return _target;
        }

        return Binding.DoNothing;
    }
} 




相关问题
热门标签