2013-09-08 1 views
0

내가 ObservableCollection에있는 경우 전, 후 프로젝트를 실행에 토글 버튼의 ​​목록을 작성 taht를 목록 상자를 가지고 내가 ObservableCollection에있는 항목은 내가 (이 항목을 확인하려면 목록 상자에있는 경우 목록 상자에서 항목이 ObservableCollection에 비교 할 토글 버튼) 내가 그렇게 tryed했지만 난 후 프로젝트를 실행 보여 토글 버튼의 ​​목록 렸기 때문에, 뒤에 코드에서 버튼을 전환 할 액세스 캔트ObservableCollection과 Toggle Button listBox를 비교합니까?

, 확인.

<ListBox x:Name="lbname" ItemsSource="{Binding source}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <ToggleButton x:Name="btnitem" Content="{Binding Name}"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ListBox> 

내 ObservableCollection에 : 한마디로

IQueryable<items> query = _context.items; 
    ocitems = new ObservableCollection<items>(query); 

: 내가 목록 상자와 ObservableCollection에 항목을 비교할 수있는 방법 (버튼) 및 항목을 목록 상자에 존재하는 경우 전환을

여기 내 목록 상자 코드입니다 항목을 나타내는 버튼이 선택되어 있습니까?

이 명확한 희망.

------------------------------------------ 더 상세

, 내가 편집 버튼을 누르면, enter image description here

<ListBox x:Name="lbChoices" ItemsSource="{Binding ocSelectedChoice}" DisplayMemberPath="ChoiceName" HorizontalAlignment="Left" Height="165" VerticalAlignment="Top" Width="186" Margin="567,50,0,0" BorderBrush="#FFC1C1C1" Background="#FFE3E3E3" SelectionMode="Extended"> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="ListBoxItem"> 
       <Setter Property="Height" Value="30"/> 
       <Setter Property="Background" Value="#FFc4d0df"/> 
       <Setter Property="BorderBrush" Value="#FFC1C1C1"/> 
       <Setter Property="BorderThickness" Value="0.8"/> 
      </Style> 
     </ListBox.ItemContainerStyle> 
    </ListBox> 

내가이 항목의 선택을 변경하려면

:

나는, ObservableCollection에 "ocSelectedChoice"에 의해 작성이 목록 상자 선택한 항목에 대한 선택을 보여이 목록 상자가 모든 가능한 선택에 대해 ObservableCollection에 의해 채워진리스트 박스가있는 윈도우를 보여줄 것입니다. 사진, 주요한 문제를보십시오. 'ch oices 1 선택 창에서 (녹색 하나) 확인 잠금 ':

enter image description here

<ItemsControl x:Name="icItemGroup" ItemsSource="{Binding PagedSource, ElementName=rdpChoices}" Margin="26,79,0,0" FontWeight="Bold" HorizontalAlignment="Left" Width="506" Height="210" VerticalAlignment="Top" > 
     <!-- ItemsPanelTemplate --> 
     <ItemsControl.ItemsPanel> 
      <ItemsPanelTemplate> 
       <UniformGrid Columns="4" HorizontalAlignment="left" /> 
      </ItemsPanelTemplate> 
     </ItemsControl.ItemsPanel> 
     <!-- ItemTemplate --> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <ToggleButton x:Name="tbtnChoices" HorizontalAlignment="Left" VerticalAlignment="Top" FontFamily="tahoma" FontSize="12" Height="45" Width="120" FontWeight="Normal" Margin="0,0,0,5" Background="#FFE8E8E8" BorderBrush="#FFC1C1C1" Foreground="#FF6A6A6A" 
           Content="{Binding ChoiceName}" TabIndex="{Binding ChoicesID}" Click="tbtnChoices_Click"> 
        <ToggleButton.IsChecked> 
         <MultiBinding Converter="{StaticResource Choices}"> 
          <Binding Path="ocChoice" RelativeSource="{RelativeSource AncestorType={x:Type Window}}"/> 
         </MultiBinding> 
        </ToggleButton.IsChecked> 
       </ToggleButton> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 

변환기 :이 만들려고

public class ChoicesConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, 
          System.Globalization.CultureInfo culture) 
    { 
     Choice _choice = values[0] as Choice; 
     ObservableCollection<Choice> ocChoices = values[1] as ObservableCollection<Choice>; 
     return ocChoices.Contains(_choice); 
    } 

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

,하지만 나를 위해 불분명 미안 여전히 뭔가주세요 내 프로젝트에서 중요하기 때문에이 문제를 해결하는 데 도움이됩니다.

답변

0

oky, 나는 여기에 내 코드입니다, 그것을 해결

public class IsSelectedChoiceConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     bool _check = false; 
     if (value == null) 
      return false; 
     Item currentItem = (Item)value; 
     if (currentItem.ChoicesinItem.Count == 0) 
      _check = false; 
     foreach (var _choicesinItem in currentItem.ChoicesinItem) 
     { 
      if (currentItem.CurrentChoiceId == _choicesinItem.ChoicesId) 
       _check = true; 
     } 
     return _check; 
    } 

및 XAML 코드 :

    <ToggleButton x:Name="tbtnChoices" HorizontalAlignment="Left" VerticalAlignment="Top" FontFamily="tahoma" FontSize="12" Height="45" Width="120" FontWeight="Normal" Margin="0,0,0,5" Background="#FFE8E8E8" BorderBrush="#FFC1C1C1" Foreground="#FF6A6A6A" 
           IsChecked="{Binding Path=Item,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource IsSelectedChoice}}" 
           Content="{Binding Item.CurrentChoiceName}" TabIndex="{Binding Item.CurrentChoiceId}" Click="tbtnChoices_Click"> 
       </ToggleButton> 

그 일이 지금, 감사를 위해입니다 누구든지 나를 도왔다.

0

당신은 즉 아래처럼 ToggleButton을의 IsChecked을 결합하는 것을 수행 할 다중 변환기를 사용한다 :

 <ListBox x:Name="lbname" ItemsSource="{Binding source}"> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <ToggleButton x:Name="btnitem" Content="{Binding Name}"> 
         <ToggleButton.IsChecked> 
          <MultiBinding Converter="{StaticResource MyConverter}"> 
           <Binding /> 
           <Binding Path="DataContext.ObservableCollectionToCompare" RelativeSource="{RelativeSource AncestorType={x:Type Window}}"/> 
          </MultiBinding> 
         </ToggleButton.IsChecked> 
        </ToggleButton> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ListBox> 

여기가 observalblecollectin 어떤 비교하려는 것을 가정하고이 뷰의 DataContext에의 한 속성입니다. MyConverter는 생성해야하는 다중 값 변환기입니다. 그리고 변환기의 Convert 메서드에서 항목이 컬렉션에 있는지 비교하고 이에 따라 false를 true로 반환 할 수 있습니다.

이 컨버터는 다음과 같습니다 :

public class MyConverter : IMultiValueConverter 
    { 
     public object Convert(object[] values, Type targetType, object parameter, 
           System.Globalization.CultureInfo culture) 
     { 

      Item listItem = values[0] as Item; 
      ObservableCollection<Item> collection = values[1] as ObservableCollection<Item>; 

      return collection.Contains(listItem); 
     } 

     public object[] ConvertBack(object value, Type[] targetTypes, object parameter, 
            System.Globalization.CultureInfo culture) 
     { 
      return null; 
     } 
    } 
+0

나는 그것을 시도 할 것이다. –

+0

제발 당신이 변환기를 만들 수 있도록 도와 주시겠습니까 –

+0

변환기를 사용하여 답변을 업데이 트 .. 그것이 도움이 되었으면 좋겠다 – Nitin