6

たくさんのスレッドを検索してみましたが、かなり近いスレッドがいくつか見つかりましたが、正確には私が望むものではありません。一日の検索の後、私はただ尋ねることに決めました。何かを逃してしまったらお詫びします。これは十分に一般的だと思いますが、なかなか理解できないようです。

ViewModelにバインドされたUserControlがあり、以下のようなViewModelのプロパティであるItemsSource=ObservableCollectionのリストボックスがあります。

アイテムを選択するたびに、特定の条件に応じて、他のいくつかのアイテムでSomeObject.IsEnabled=falseを呼び出します。リストボックスの項目をそのIsEnabledプロパティにバインドして、選択を行うたびにどの項目もグレーアウトできるようにします。

ViewModel:

Class ViewModel : PropertyNotificationObject
{
    private ObservableCollection<SomeObject> m_list;
    public ObservableCollection<SomeObject> List {get; set;} //notifying properly

    private void selectedItem()
    {
        //several in SomeObjects in List sets IsEnabled = false
    }
} 

オブジェクトクラス

class SomeObject : PropertyNotificationObject
{
   private bool m_isEnabled;
   public IsEnabled { get; set; } //notifying properly
}

XAML

 <DataTemplate x:Key="ListTemplate">
    <Grid>
       <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*"/>
       </Grid.ColumnDefinitions>
       <TextBlock Text="{Binding ., Converter={someConverterObjectToString}}"/>
    </Grid>
 </DataTemplate>
 <ListBox ItemsSource="{Binding List}" ItemTemplate="{StaticResource ListTemplate}"/>

以下のようにListBox.ItemContainerStyleとDataTemplateでStyleTriggersを使用してみましたが、SomeOject.IsEnabledプロパティにアクセスする方法がわかりませんでした。

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <DataTrigger Binding={???????? I can't get to my SomeObject properties.}
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>

色が足りないので申し訳ありませんが、私はここが初めてで、エディターの使い方がよくわかりません。

前もって感謝します。

4

1 に答える 1

14

{Binding IsEnabled}あなたの中ItemContainerStyleで仕事をする必要があります。バインディングエラーについては、VSデバッグウィンドウを確認してください

ListBoxItemのIsEnabledプロパティを編集するか、直接バインドします。

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
</Style>
于 2012-12-07T19:21:55.557 に答える