2011-02-25 91 views
1
<StackPanel Grid.Column="0" > 
      <Button Name="buttonEditListBoxItem" Content="Edit" Click="buttonEditListBoxItem_Click"></Button> 
      <ListBox Name="ListBoxTriggers" 
       SelectedValuePath="TriggerId" 
       IsSynchronizedWithCurrentItem="True" 
       SelectionChanged="Triggers_SelectionChanged" 
       ScrollViewer.HorizontalScrollBarVisibility="Auto" 
       HorizontalContentAlignment="Stretch"> 
      <ListBox.ItemTemplate> 
       <DataTemplate><Grid > 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="AUTO"/> 
           <ColumnDefinition Width="*"/> 
          </Grid.ColumnDefinitions> 
          <CheckBox Grid.Column="0" VerticalAlignment="Center" x:Name="checkBoxTriggers" ></CheckBox> 
          <Button Grid.Column="1" Style="{StaticResource GlassButton}" 
          Uid="{Binding Path=TriggerId}" 
          Margin="5" 
          x:Name="ButtonTrigger" 
          GotFocus="ButtonTrigger_GotFocus" 
          > 
           <Button.Content> 
            <TextBlock Foreground="White" TextAlignment="Justify" 
             TextWrapping="Wrap" Margin="6" Text="{Binding Path=Name}"/> 
           </Button.Content> 
          </Button> 
         </Grid> 
        </DataTemplate> 
       </ListBox.ItemTemplate></ListBox> 

      </StackPanel> 

我想創建一個包含按鈕和複選框的數據模板的列表框。當我的編輯按鈕,這些複選框都應該得到看得見點擊,當我點擊它,我再次想使這些複選框看不見什麼,我想要做的就是/摺疊將複選框添加到wpf列表框中以進行多項選擇

答:我已經改變了代碼,現在這樣:`

</Window.Resources> <StackPanel Grid.Column="0" > 
      <ToggleButton Name="buttonEditListBoxItem" 
          Content="Edit" 
          IsChecked="False" 
          Click="buttonEditListBoxItem_Click"></ToggleButton> 
      <ListBox Name="ListBoxTriggers" 
       SelectedValuePath="TriggerId" 
       IsSynchronizedWithCurrentItem="True" 
       SelectionChanged="Triggers_SelectionChanged" 
       ScrollViewer.HorizontalScrollBarVisibility="Auto" 
       HorizontalContentAlignment="Stretch"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> <Grid > 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition Width="AUTO"/> 
           <ColumnDefinition Width="*"/> 
          </Grid.ColumnDefinitions> 
          <CheckBox Visibility="{Binding ElementName=buttonEditListBoxItem, Path=IsChecked, Converter={StaticResource visibilityConverter}}" Grid.Column="0" VerticalAlignment="Center" x:Name="checkBoxTriggers" ></CheckBox> 
          <Button Grid.Column="1" Style="{StaticResource GlassButton}" 
          Uid="{Binding Path=TriggerId}" 
          Margin="5" 
          x:Name="ButtonTrigger" 
          GotFocus="ButtonTrigger_GotFocus" 
          > 
           <Button.Content> 
            <TextBlock Foreground="White" TextAlignment="Justify" 
             TextWrapping="Wrap" Margin="6" Text="{Binding Path=Name}"/> 
           </Button.Content> 
          </Button> 
         </Grid> 
        </DataTemplate> </ListBox.ItemTemplate> 
     </ListBox>` `public class VisibilityConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return ((Boolean)value) ? Visibility.Visible : Visibility.Collapsed; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return (((Visibility)value) == Visibility.Visible); 
    } 
}` 

回答

1

以實現最簡單的方法是使用一個切換按鈕

例如:

<ToggleButton Name="Toggler"> 
</ToggleButton> 
<StackPanel Visibility="{Binding ElementName=Toggler, Path=Checked, Converter={StaticResource booleanToVisibilityConverter}}"> 
<!-- in here you can place the checkboxes--> 
</StackPanel> 

你只需要創建從布爾值轉換爲枚舉能見度

另一個valueconverter可能性是將一個「Popup」元素與一個ToggleButton結合使用(它給出了一個彈出來編輯該項目的小助手的感覺,並且如果設置了StaysOpen =「False」,則在丟失f時立即關閉ocus) 如果您使用這種方法,您不必壓制編輯按鈕來隱藏複選框

+0

感謝您的回答。我添加了一個ToggleButton,並使用其可見性轉換器的IsChecked屬性來決定複選框的可見性。 – batrulz 2011-02-25 20:08:06

+0

想問一個後續問題....如果我必須使用這個作爲一個控件,並使用屬性buttonEditListBoxItem.isVisible = false ....我將它轉換成一個用戶控件/自定義控件,以便我可以使用它在任何窗口 – batrulz 2011-02-25 20:23:42

1

如何使用ToggleButton代替ButtonVisibilityConverter

<ListBox Name="ListBoxTriggers" 
      SelectedValuePath="TriggerId" 
      IsSynchronizedWithCurrentItem="True" 
      SelectionChanged="Triggers_SelectionChanged" 
      ScrollViewer.HorizontalScrollBarVisibility="Auto" 
      HorizontalContentAlignment="Stretch"> 
    <ListBox.Resources> 
     <local:VisibilityConverter x:Key="VisibilityConverter" /> 
    </ListBox.Resources> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="AUTO" /> 
        <ColumnDefinition Width="*" /> 
       </Grid.ColumnDefinitions> 
       <CheckBox Grid.Column="0" 
          VerticalAlignment="Center" 
          x:Name="checkBoxTriggers" 
          Visibility="{Binding ElementName=ButtonTrigger, Path=IsChecked, Converter={StaticResource VisibilityConverter}}"></CheckBox> 
       <ToggleButton Grid.Column="1" 
           Uid="{Binding Path=TriggerId}" 
           Margin="5" 
           x:Name="ButtonTrigger" 
           GotFocus="ButtonTrigger_GotFocus"> 
        <ToggleButton.Content> 
         <TextBlock Foreground="White" 
            TextAlignment="Justify" 
            TextWrapping="Wrap" 
            Margin="6" 
            Text="{Binding Path=Name}" /> 
        </ToggleButton.Content> 
       </ToggleButton> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

VisibilityConverter:

public class VisibilityConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return ((Boolean)value) ? Visibility.Visible : Visibility.Collapsed; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return (((Visibility)value) == Visibility.Visible); 
    } 
} 
+0

感謝您的回答。我添加了一個ToggleButton,並使用其可見性轉換器的IsChecked屬性來決定複選框的可見性。 – batrulz 2011-02-25 20:08:25

+0

我不想讓模板內的按鈕決定我的複選框的可見性。在列表框外有一個編輯按鈕,我想根據它來決定。謝謝雖然你的答案確實有幫助。 – batrulz 2011-02-25 20:14:15

相關問題