2015-10-01 11 views
9

मेरे पास एक डब्ल्यूपीएफ डाटाग्रिड है जिसमें संग्रह दृश्य स्रोत है जिसमें समूह के 3 स्तर हैं।सभी सामग्री ध्वस्त होने पर छुपाएं

मैं datagrid 3 विस्तारक ऐसी है कि वह इस तरह दिखता है उपयोग करने के लिए स्टाइल है:

Level 1 Expander 
<content> 
    Level 2 Expander 
    <content> 
     Level 3 Expander 
     <content> 

स्तर 2 और स्तर 1 बस समूहों का खिताब हैं

मैं एक दूसरे नियंत्रण है कि अनुमति देता है उपयोगकर्ता 3 स्तरों को दिखाने और छिपाने के लिए जो ऑब्जेक्ट में एक बूलियन "IsVisible" संपत्ति को स्तर 3 विस्तारक को बाध्य करके काम करता है।

 <!-- Style for groups under the top level. this is the style for how a sample is displayed --> 
     <GroupStyle> 
      <GroupStyle.ContainerStyle> 
       <Style TargetType="{x:Type GroupItem}"> 
        <Setter Property="Margin" Value="0,0,0,0" /> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="{x:Type GroupItem}"> 

           <!-- The parent control that determines whether or not an item needs to be displayed. This holds all of the sub controls displayed for a sample --> 
           <Expander Margin="2" 
              Background="{Binding Path=Name, 
                   Converter={StaticResource SampleTypeToColourConverter}}" 
              IsExpanded="True" 
              Visibility="{Binding Path=Items[0].IsVisibleInMainScreen, 
                   Converter={StaticResource BoolToVisibilityConverter}}"> 

यह दृष्टिकोण fantasically अच्छी तरह से काम करता है।

हालांकि

उपयोगकर्ता एक स्तर 3 विस्तारक में सभी आइटम deselects हैं, स्तर 2 विस्तारक हेडर अभी भी अर्थ प्रदर्शित करता है मूल्यवान अचल संपत्ति नहीं दिखाई डेटा के साथ एक समूह के शीर्षक दिखा ऊपर प्रयोग किया जाता है।

क्या मैं चाहते हैं अपने बच्चे को नियंत्रण करने के लिए स्तर 2 विस्तारक की दृश्यता को बाध्य करने और कहने के लिए एक रास्ता है "सभी बच्चों को दिखाई दे रहे हैं तो विस्तारक दिखाते हैं, अन्यथा यह पतन"

यह संभव है?

+1

* यदि सभी बच्चे दिखाई दे रहे हैं तो विस्तारक दिखाएं, अन्यथा इसे संकुचित करें * कनवर्टर कार्य की तरह लगता है। आपके पास पहले से ही संपत्ति है 'IsVisibleInMainWindow', बच्चों को ध्वस्त होने पर इसे बदलें। नोट: 'आइटम' 'पर्यवेक्षण चयन 'होना चाहिए। – Sinatr

+0

क्या आप हमें सभी विस्तारक समेत एक पूर्ण पूर्ण xaml दे सकते हैं? –

+0

मुझे लगता है कि हमें और अधिक xaml की आवश्यकता है। – PScr

उत्तर

3

में पहला आइटम परिवर्तित करने के लिए इस्तेमाल किया। यदि आपके पास बहुत अधिक समूह नहीं हैं तो यह चाल चलनी चाहिए।

मैं सिर्फ GroupItem ControlTemplate को यह ट्रिगर जोड़ दिया है:

<ControlTemplate.Triggers> 
    <DataTrigger Binding="{Binding ElementName=IP, Path=ActualHeight}" Value="0"> 
     <Setter Property="Visibility" Value="Hidden"/> 
     <Setter Property="Height" Value="1"/> 
    </DataTrigger> 
</ControlTemplate.Triggers> 

जब ItemsPresenter (आईपी) ActualSize शून्य करने के लिए चला जाता है, यह लगभग हैडर पतन होगा।

क्यों लगभग?

नियंत्रण प्रारंभ हो जाता है जब और इससे पहले कि बाध्यकारी होता है, ItemPresenter ActualHeight 0 और जब VisibilityCollapsed के लिए सेट है, ItemPresenter सब पर गाया नहीं होती है।

Visibility.Hidden का उपयोग ItemsPresenter को रेंडर चरण पर जाने और मिश्रित होने की अनुमति देता है। मुझे Height से .4 पीएक्स ड्रॉप करने के लिए सफलता मिली लेकिन मुझे संदेह है कि यह डिवाइस निर्भर है।

+0

मैंने यह जवाब चुना क्योंकि यह शुद्ध xaml में बॉक्स के लगभग पूरी तरह से काम करता था। ऐसा करने का यह सबसे आसान तरीका है, मुझे लगता है कि हालांकि दूसरों के उत्तर में अपने अधिकार में योग्यता है –

1

यह प्रत्यक्ष जवाब नहीं है क्योंकि आपको इसे विशेष रूप से अपनी आवश्यकताओं के लिए लागू करना होगा, लेकिन पहले मैंने सदस्यों के गतिशील ग्रिड आवंटन को बनाने के लिए ग्रिड नियंत्रण का ओवरराइड उपयोग किया है, यदि कोई दृश्य सदस्य नहीं है तो अभिभावक समूह बॉक्स छुपाता है।

public bool AreChildrenVisible { get { return _children.Any(x=>x.IsVisibleInMainScreen); } } 

वैकल्पिक रूप से, पास:

public class DynamicLayoutGrid : Grid 
{ 

     protected override void OnInitialized(EventArgs e) 
     { 
       //Hook up the loaded event (this is used because it fires after the visibility binding has occurred) 
      this.Loaded += new RoutedEventHandler(DynamicLayoutGrid_Loaded); 

      base.OnInitialized(e); 
     } 


     void DynamicLayoutGrid_Loaded(object sender, RoutedEventArgs e) 
     { 
      int numberOfColumns = ColumnDefinitions.Count; 
      int columnSpan = 0; 
      int rowNum = 0; 
      int columnNum = 0; 
      int visibleCount = 0; 

      foreach (UIElement child in Children) 
      { 
       //We only want to layout visible items in the grid 
       if (child.Visibility != Visibility.Visible) 
       { 
        continue; 
       } 
       else 
       { 
        visibleCount++; 
       } 

       //Get the column span of the element if it is not in column 0 as we might need to take this into account 
       columnSpan = Grid.GetColumnSpan(child); 

       //set the Grid row of the element 
       Grid.SetRow(child, rowNum); 

       //set the grid column of the element (and shift it along if the previous element on this row had a rowspan greater than 0 
       Grid.SetColumn(child, columnNum); 

       //If there isn't any columnspan then just move to the next column normally 
       if (columnSpan == 0) 
       { 
        columnSpan = 1; 
       } 

       //Move to the next available column 
       columnNum += columnSpan; 

       //Move to the next row and start the columns again 
       if (columnNum >= numberOfColumns) 
       { 
        rowNum++; 
        columnNum = 0; 
       } 
      } 

      if (visibleCount == 0) 
      { 
       if (this.Parent.GetType() == typeof(GroupBox)) 
       { 
        (this.Parent as GroupBox).Visibility = Visibility.Collapsed; 
       } 
      } 
     } 
    } 
3

यह मानते हुए कि आप शैली का एक MVVM तरह प्रयोग कर रहे हैं, आप अपने समूह वस्तु की एक संपत्ति है कि झूठी लौटाता है यदि बच्चों के सभी अदृश्य हैं के बजाय बाँध सकता है समूह में सभी उप-समूहों की कुल स्थिति के आधार पर एक कनवर्टर क्लास के माध्यम से वस्तुओं का संग्रह दृश्यता वापस करने के लिए।

0

वस्तुओं को दृश्यता में बदलने के लिए IMultiValueConverter कार्यान्वयन का उपयोग करें। यदि सभी आइटम IsVisibleInMainScreen प्रॉपर्टी वापस लौटते हैं तो कनवर्टर दृश्यमान और छुपा दिखाई देगा।

एक ही स्थान पर कनवर्टर का उपयोग करें यू मैं बल्कि एक सरल और साफ रास्ता है, फिर भी सही नहीं है, अपने लक्ष्य को प्राप्त करने के लिए मिला मूल उदाहरण

संबंधित मुद्दे