2010-02-26 14 views
5

के लिए पाठ मैं एक कॉम्बो बॉक्स है कि एक डेटा टेम्पलेट पर आधारित है इस तरह की तरह चेक बॉक्स शामिल है:'डिफ़ॉल्ट' टेम्प्लेटेड कॉम्बो बॉक्स

<ComboBox x:Name="cboComplex" Text="Select days..."> 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <CheckBox IsChecked="{Binding Path=IsSelected}" Width="20"/> 
        <TextBlock Text="{Binding DayOfWeek}" Width="100" /> 
       </StackPanel> 
      </DataTemplate> 

     </ComboBox.ItemTemplate> 
    </ComboBox> 

समस्या मैं आ रही हैं कि मैं चाहता है "दिन चुनें ..." प्रदर्शित करने के लिए combobox और फिर क्लिक करते समय सूची दिखाएं। दुर्भाग्य से टेक्स्ट प्रॉपर्टी को सेट करने का कोई प्रभाव नहीं पड़ता है। किसी भी विचार या मदद की सराहना की जाएगी।

अग्रिम धन्यवाद!

Sieg

उत्तर

2

आप, मूल्य "का चयन करें दिनों ..." के साथ अपनी अंतर्निहित संग्रह कक्षा में एक नया आइटम बनाने के लिए साथ सूचकांक [0] और 0.

<ComboBox x:Name="cboComplex" SelectedIndex="0"> 
     <ComboBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <CheckBox IsChecked="{Binding Path=IsSelected}" Width="20"/> 
        <TextBlock Text="{Binding DayOfWeek}" Width="100" /> 
       </StackPanel> 
      </DataTemplate> 

     </ComboBox.ItemTemplate> 
    </ComboBox> 
करने के लिए चयनित सूचकांक बदल होगा

या दूसरा विकल्प टेक्स्ट के साथ "लेबल चुनें ..." टेक्स्ट के साथ एक लेबल डालना है, और फिर ऑनसेलेक्शन चेंजेड ईवेंट को सुनें, और यदि चयनित इंडेक्स -1 नहीं है, तो लेबल दृश्यता को झूठी में बदलें, अन्यथा सच है। जैसे

private void MyListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     if (MyListBox.SelectedIndex >= 0) 
     { 
      MyListBoxInitialLabel.Visibility = Visibility.Hidden; 
     } 
     else 
     { 
      MyListBoxInitialLabel.Visibility = Visibility.Visible; 
     } 
    } 
5

कॉम्बोबॉक्स संपादन योग्य होने पर टेक्स्ट प्रॉपर्टी का उपयोग किया जाता है। आप ControlTemplate में कोई तत्व जोड़कर डिफ़ॉल्ट "चयन करें" प्रकार संदेश सेट कर सकते हैं जो केवल तब दिखाई देता है जब कोई चयन न हो और फिर गायब हो जाए। इस विधि का उपयोग करने के लिए आपको अपने संग्रह को संशोधित करने या उपयोगकर्ता को सूची से "चयन" संदेश लेने का प्रयास करने की आवश्यकता नहीं है क्योंकि यह सूची का हिस्सा नहीं है।

<TextBlock x:Name="SelectMessage" HorizontalAlignment="Left" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}" VerticalAlignment="Center" Margin="{TemplateBinding Padding}" Visibility="Collapsed"/> 

तो फिर तुम इसे इस तरह इस्तेमाल कर सकते हैं:

मैं टैग का उपयोग कर प्रत्येक उदाहरण पर या एक शैली में अपना संदेश स्थापित करने के लिए और फिर इसे डिफ़ॉल्ट टेम्पलेट में प्रदर्शित करने के लिए एक नया TextBlock जोड़ने की सलाह देते हैं
<ComboBox ItemsSource="{Binding MyList}" Template="{StaticResource ComboBoxMessageTemplate}" Tag="Select days..."/> 

परिवर्तन के साथ डिफ़ॉल्ट एयरो कॉम्बोबॉक्स टेम्पलेट की एक पूर्ण मिश्रण उत्पन्न प्रति है।

<Geometry x:Key="DownArrowGeometry">M 0 0 L 3.5 4 L 7 0 Z</Geometry> 
<Style x:Key="ComboBoxReadonlyToggleButton" TargetType="{x:Type ToggleButton}"> 
    <Setter Property="OverridesDefaultStyle" Value="true"/> 
    <Setter Property="IsTabStop" Value="false"/> 
    <Setter Property="Focusable" Value="false"/> 
    <Setter Property="ClickMode" Value="Press"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ToggleButton}"> 
       <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}"> 
        <Grid HorizontalAlignment="Right" Width="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}"> 
         <Path x:Name="Arrow" Fill="Black" HorizontalAlignment="Center" Margin="3,1,0,0" VerticalAlignment="Center" Data="{StaticResource DownArrowGeometry}"/> 
        </Grid> 
       </Microsoft_Windows_Themes:ButtonChrome> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsChecked" Value="true"> 
         <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/> 
        </Trigger> 
        <Trigger Property="IsEnabled" Value="false"> 
         <Setter Property="Fill" TargetName="Arrow" Value="#AFAFAF"/> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<ControlTemplate x:Key="ComboBoxMessageTemplate" TargetType="{x:Type ComboBox}"> 
    <Grid x:Name="MainGrid" SnapsToDevicePixels="true"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition MinWidth="{DynamicResource {x:Static SystemParameters.VerticalScrollBarWidthKey}}" Width="0"/> 
     </Grid.ColumnDefinitions> 
     <Popup x:Name="PART_Popup" Margin="1" AllowsTransparency="true" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}" Placement="Bottom" PopupAnimation="{DynamicResource {x:Static SystemParameters.ComboBoxPopupAnimationKey}}" Grid.ColumnSpan="2"> 
      <Microsoft_Windows_Themes:SystemDropShadowChrome x:Name="Shdw" MaxHeight="{TemplateBinding MaxDropDownHeight}" MinWidth="{Binding ActualWidth, ElementName=MainGrid}" Color="Transparent"> 
       <Border x:Name="DropDownBorder" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" BorderBrush="{DynamicResource {x:Static SystemColors.WindowFrameBrushKey}}" BorderThickness="1"> 
        <ScrollViewer CanContentScroll="true"> 
         <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" KeyboardNavigation.DirectionalNavigation="Contained"/> 
        </ScrollViewer> 
       </Border> 
      </Microsoft_Windows_Themes:SystemDropShadowChrome> 
     </Popup> 
     <ToggleButton Style="{StaticResource ComboBoxReadonlyToggleButton}" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" Grid.ColumnSpan="2" IsChecked="{Binding IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/> 
     <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" IsHitTestVisible="false" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Content="{TemplateBinding SelectionBoxItem}" ContentStringFormat="{TemplateBinding SelectionBoxItemStringFormat}" ContentTemplate="{TemplateBinding SelectionBoxItemTemplate}" ContentTemplateSelector="{TemplateBinding ItemTemplateSelector}"/> 
     <TextBlock x:Name="SelectMessage" HorizontalAlignment="Left" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag}" VerticalAlignment="Center" Margin="{TemplateBinding Padding}" Visibility="Collapsed"/> 
    </Grid> 
    <ControlTemplate.Triggers> 
     <Trigger Property="SelectedItem" Value="{x:Null}"> 
      <Setter Property="Visibility" TargetName="SelectMessage" Value="Visible"/> 
     </Trigger> 
     <Trigger Property="HasDropShadow" SourceName="PART_Popup" Value="true"> 
      <Setter Property="Margin" TargetName="Shdw" Value="0,0,5,5"/> 
      <Setter Property="Color" TargetName="Shdw" Value="#71000000"/> 
     </Trigger> 
     <Trigger Property="HasItems" Value="false"> 
      <Setter Property="Height" TargetName="DropDownBorder" Value="95"/> 
     </Trigger> 
     <Trigger Property="IsEnabled" Value="false"> 
      <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
      <Setter Property="Background" Value="#FFF4F4F4"/> 
     </Trigger> 
     <Trigger Property="IsGrouping" Value="true"> 
      <Setter Property="ScrollViewer.CanContentScroll" Value="false"/> 
     </Trigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 
+1

+1 धन्यवाद, अच्छा जवाब। टेक्स्टब्लॉक में जोड़ने के लिए बस एक अतिरिक्त चीज़ है HitTestVisible = "गलत"। –

+0

शुद्ध कामकाज, शुद्ध एक्सएएमएल और कोई पाठ संपत्ति दुरुपयोग दोनों, चारों ओर जीत :) – dain

2

दिखाएँ डिफ़ॉल्ट: और PresentationFramework.Aero विधानसभा के लिए एक संदर्भ, तुम भी विषय नाम स्थान (: Microsoft_Windows_Themes = "clr-नाम स्थान विधानसभा = PresentationFramework.Aero Microsoft.Windows.Themes" xmlns) की आवश्यकता होगी कॉम्बो बॉक्स में पाठ

<ComboBox Height="23" HorizontalAlignment="Left" Margin="180,18,0,0" Name="cmbExportData" VerticalAlignment="Top" Width="148" ItemsSource="{Binding}" Text="-- Select Value --" AllowDrop="False" IsEditable="True" IsManipulationEnabled="False" IsReadOnly="True" /> 
  • कॉम्बो बॉक्स का पाठ संपत्ति यह IsEditable = true
  • के रूप में यह ०१२३५२४०४१ के रूप में सेट
  • मार्क मार्क