2013-02-18 118 views
4

我已經重新風格的列表框,從這個linkWPF RadioButtonList的同時設置的IsEnabled和器isChecked從集合

我的要求創建一個單選按鈕列表屬性是這樣的:我在來從列表框中選擇一個項目一段時間(即單人選擇模式)。此外,我必須禁用/啓用基於綁定到集合的屬性的ListBoxItem。所以我已經設置

IsChecked="{TemplateBinding IsSelected}" 

從我收集綁定的的IsEnabled財產。

IsEnabled="{Binding IsEnabled}" 

而結果如下: enter image description here

你可以看到,一些記錄是禁用狀態,但他們仍然是selectable.If我刪除器isChecked屬性,它可以完美如預期。但我同時需要IsEnabled & IsSelected功能。 然後,我爲IsEnabled屬性創建一個多值轉換器,並基於這些值將相應的值綁定到屬性。 現在我無法從列表中直觀地選擇禁用的項目。但是,當我選擇一個禁用的項目,我失去了選擇。請檢查圖像: enter image description here

並在代碼後面的IsChecked屬性設置爲第一條記錄。 我想限制這個選擇。我怎樣才能做到這一點? xaml中有任何設置將有助於滿足我的要求嗎?請指點...提前

感謝....

回答

1

這聽起來像你綁定RadioButton而不是ListBoxItemIsEnabled財產。這將禁用RadioButton,但不是ListBoxItem,這就是它仍然可以被選中的原因。

您應該可以綁定ListBoxItemIsEnabled屬性,並且它會按照您的方式工作。

根據您發佈要你使用的風格的鏈接,這將是在RadioButtonList風格的ItemContainerStyle部分:

<Style TargetType="{x:Type ListBoxItem}" > 
    <!-- Here --> 
    <Setter Property="IsEnabled" Value="{Binding IsEnabled}" /> 

    <Setter Property="Margin" Value="5" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Border BorderThickness="0" Background="Transparent"> 
        <RadioButton Focusable="False" 
           IsHitTestVisible="False" 
         IsChecked="{TemplateBinding IsSelected}"> 
         <ContentPresenter /> 
        </RadioButton> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style>