【问题标题】:WPF Combobox editor style binding is not workingWPF 组合框编辑器样式绑定不起作用
【发布时间】:2021-04-23 03:43:25
【问题描述】:

我有以下 xaml 代码。

我正在尝试将自定义类 UnitCategory 绑定到组合框。

但似乎代码有问题。

 <Grid>
    <Grid.Resources>
        <local:ComboSourceConverter x:Key="cscconv" />

        <Style x:Key="UnitCategoryStyle" TargetType="{x:Type igEditors:XamComboEditor}">
            <Setter Property="ItemsSource" Value="{Binding Path=DataContext.UnitCategories, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
            <Setter Property="SelectedItem" Value ="{Binding Path=DataItem.UnitCategory}" />
            <Setter Property="DisplayMemberPath" Value="Name" />
        </Style>
        
        <Style x:Key="UnitStyle" TargetType="{x:Type igEditors:XamComboEditor }">
            <Setter Property="ItemsSource" Value="{Binding Path=DataItem.UnitCategory, Converter={StaticResource cscconv}}" />
        </Style>

    </Grid.Resources>
    <igWPF:XamDataGrid DataSource="{Binding data}" CellChanged="XamDataGrid_CellChanged">
        <igWPF:XamDataGrid.FieldLayoutSettings>
            <igWPF:FieldLayoutSettings AutoGenerateFields="False" />
        </igWPF:XamDataGrid.FieldLayoutSettings>
        <igWPF:XamDataGrid.FieldLayouts>
            <igWPF:FieldLayout>
                <igWPF:FieldLayout.Fields>
                    <igWPF:Field Name="Item"></igWPF:Field>
                    <igWPF:Field Name="UnitCategory">
                        <igWPF:Field.Settings>
                            <igWPF:FieldSettings EditorStyle="{StaticResource UnitCategoryStyle}" />
                        </igWPF:Field.Settings>
                    </igWPF:Field>
                    <igWPF:Field Name="Unit">
                        <igWPF:Field.Settings>
                            <igWPF:FieldSettings EditorStyle="{StaticResource UnitStyle}" />
                        </igWPF:Field.Settings>
                    </igWPF:Field>
                </igWPF:FieldLayout.Fields>
            </igWPF:FieldLayout>
        </igWPF:XamDataGrid.FieldLayouts>
    </igWPF:XamDataGrid>
</Grid>

跟随视图模型。此处单元类别不具有约束力。组合框中的下拉菜单未显示。有绑定问题吗?

public class ViewModel : INotifyPropertyChanged
{
    public ObservableCollection<DataModel> data { get; set; }
    public List<UnitCategory> UnitCategories { get; set; }
    public static List<string> WeightUnits { get; set; }
    public static List<string> DistanceUnits { get; set; }

    public ViewModel()
    {
        this.data = new ObservableCollection<DataModel>();

        this.UnitCategories = new List<UnitCategory>();

        UnitCategory u1 = new UnitCategory();
        u1.Name = "Wt";

        UnitCategory u2 = new UnitCategory();
        u2.Name = "Dt";


        this.UnitCategories.Add(u1);
        this.UnitCategories.Add(u2);



        WeightUnits = new List<string>()
        {
            "mg",
            "g",
            "kg"
        };

        DistanceUnits = new List<string>()
        {
            "cm",
            "m",
            "km"
        };

        DataModel data1 = new DataModel();
        data1.Item = "Item 1";
      
        DataModel data2 = new DataModel();
        data2.Item = "Item 2";
 
        this.data.Add(data1);
        this.data.Add(data2);
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

public class DataModel 
{
    public string Item { get; set; }
    public UnitCategory UnitCategory { get; set; }
    public string Unit { get; set; }

    public DataModel()
    {
    }
}

public class UnitCategory
{
    public string Name { get; set; }
}

【问题讨论】:

  • 尝试将AncestorType={x:Type Window}更改为AncestorType={x:Type igWPF:XamDataGrid}
  • 它不工作。实际上我们需要获取窗口数据上下文。因为对于 window -> VIewMODEl 是 datacontext。对于 Xamdatagrid -> 模型是数据上下文。对于组合框,它应该取自 viewmodel,它是 windows datacontext
  • 对于igWPF:XamDataGrid DataContext 是/应该是ViewModel,因为你绑定了DataSource="{Binding data}" 或者你发布了错误的代码。
  • 对于 XamdataGrid DataSource 是 DataModel 的列表,它是数据。对于窗口 Datacontext 是 viewModel 的实例
  • 不,DataModel 的列表不是igWPF:XamDataGrid 的DataContext,而是DataSource。

标签: c# wpf datagrid infragistics


【解决方案1】:

我想igWPF:Field 或其某些部分不在可视化树中。尝试创建一个ViewModel 作为静态资源,然后绑定到StaticResource。

<Window ... DataContext="{DynamicResource vmod}>
    <Window.Resources>
        <local:ViewModel x:Key="vmod" x:Shared="true"></local:ViewModel>"
    </Window.Resources>

    <Grid>
        <Grid.Resources>
            <Style x:Key="UnitCategoryStyle" TargetType="{x:Type igEditors:XamComboEditor}">
                <Setter Property="Tag" Value ="{StaticResource vmod}"
                <Setter Property="ItemsSource" Value="{Binding Tag.UnitCategories, RelativeSource={RelativeSource Mode=Self}}" />
                <Setter Property="SelectedItem" Value ="{Binding Path=DataItem.UnitCategory}" />
                <Setter Property="DisplayMemberPath" Value="Name" />
            </Style>
        </Grid.Resources>
    <Grid>

  ...

</Window>

不要忘记删除您之前设置的DataContext。

【讨论】:

  • 不幸的是,它不起作用@Rekshino。我不明白问题出在哪里。似乎一切都是正确的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-10
  • 1970-01-01
  • 2018-07-27
  • 2014-12-23
相关资源
最近更新 更多