0

VS 2012で、Code FirstEntityFrameworkとリポジトリおよびMVVMパターンを使用するWPFアプリケーションを構築しています。

EntityTypeとLicenseTypeの2つのクラスがあります。

EntityTypeは、次のように指定されます。

    private int _id;
    private string _name;
    private string _description;
    private List<LicenseType> _licenseTypes = new List<LicenseType>();

LicenseTypeは次のように宣言されます:

    private int _id;
    private string _name;
    private string _description;

そして、これらのプライベートフィールドに対して宣言されたパブリックプロパティがあります。

ObservableCollectionをCheckedListBox(Syncfusion製品)にバインドしています。

問題ない。

その下で、別のCheckedListBoxをObservableCollectionにバインドしています。

繰り返しますが、問題ありません。

私ができないことは、その特定のLicenseTypeがEntityType.LicenseTypesのコレクションにあるかどうかに基づいて、LicenseTypeリストボックスのCheckListBoxItemのIsSelectedプロパティをtrueに設定することです(これにより、チェックボックスを「チェック」します)。

CheckedListBoxのコードは次のとおりです。

         <syncfusion:CheckListBox x:Name="lstAllLicenseTypes" ItemsSource="{Binding   AllLicenseTypes}" DisplayMemberPath="Name" >
           <syncfusion:CheckListBox.ItemContainerStyle>
                <Style TargetType="syncfusion:CheckListBoxItem">
                    <Setter Property="IsSelected" Value="{Binding ????}"/>
                </Style>
            </syncfusion:CheckListBox.ItemContainerStyle>
        </syncfusion:CheckListBox>

2つの方法を試しました。最初の方法は、LicenseTypeリストボックスの各項目をループし、EntityTypeのLicenseTypeコレクションとの関連付けがあるかどうかを確認し、XAMLにバインドされたプロパティ値を設定することです。

private void ListEntityTypes_OnSelectionChanged(object sender、SelectionChangedEventArgs e){var thisEntity =(EntityType)listEntityTypes.SelectedItems [0];

        foreach (object t in lstAllLicenseTypes.Items)
        {
            bool hasAssociation = _vmEntityTypes.EntityHasAssociationWithThisLicenseType(thisEntity,(LicenseType) t);
            if (hasAssociation)
            {
                _vmEntityTypes.CurrentEntityIsAssociatedWithCurrentLicenseType = true;
            }
            else
            {
                _vmEntityTypes.CurrentEntityIsAssociatedWithCurrentLicenseType = false;
            }
        }

    }

ここでの目的は、プロパティ「CurrentEntityIsAssociatedWithCurrentLicenseType」にバインドすることです。これは機能しませんでした。CheckedListBoxの各項目についてプロパティ値が更新されたため、最終的にはfalseであり、項目がチェックされなかったため、機能しなかったと思います。

他のアプローチは上記と同様でしたが、リストボックスアイテムを手動で「チェック」しようとしましたが、LicenseTypeをCheckListItemにキャストして戻すことはできませんでした。

一般に、私が苦手なのは、他のデータコレクションに関連するレコードのコレクションを操作し、ユーザーがさまざまなテーブルを明確に追加して関連付けることができるインターフェイスを構築することです。

どんな助けも素晴らしいでしょう、

ありがとう!ポール

4

1 に答える 1

-1

コンバーターを使用してください。リストをラップするプロパティ (EntityType.LicenseTypes) に IsChecked をバインドし、ブール値を返す IValueConverter を使用します。CheckedListBoxItem の LicenseType を ConverterParameter として渡します。

EntityType クラスに IPropertyNofity を実装します。

EntityType.LicenseTypes リストが変更されると、ラップされたプロパティで PropertyNotify を使用すると、isChecked が更新されます。

疑似コード:

public class EntityType : INotifyPropertyChanged
{
    ...
    public List<LicenseType> LicenseTypes
    { 
        get { return _licenseTypes; }
        set
        {
            _licenseTypes = value;
            OnNotifyPropertyChanged("LicenseTypes");
        }
    }

}

public class ListToCheckedConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter ...
    {
        var list = (List<LicenseType>)value;
        var type = (LicenseType)parameter;
        return list.Contains(type);
    }
    ...
}

XAML:

    <syncfusion:CheckListBox x:Name="lstAllLicenseTypes" ItemsSource="{Binding   AllLicenseTypes}" DisplayMemberPath="Name" >
       <syncfusion:CheckListBox.ItemContainerStyle>
            <Style TargetType="syncfusion:CheckListBoxItem">
                <Setter Property="IsSelected" 
                    Value="{Binding Path=LicenseTypes, 
                            Converter = ListToCheckedConverter,
                            ConverterParameter = Header}"/>
            </Style>
        </syncfusion:CheckListBox.ItemContainerStyle>
    </syncfusion:CheckListBox>
于 2013-02-14T23:06:52.447 に答える