<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);
}
}`
感謝您的回答。我添加了一個ToggleButton,並使用其可見性轉換器的IsChecked屬性來決定複選框的可見性。 – batrulz 2011-02-25 20:08:06
想問一個後續問題....如果我必須使用這個作爲一個控件,並使用屬性buttonEditListBoxItem.isVisible = false ....我將它轉換成一個用戶控件/自定義控件,以便我可以使用它在任何窗口 – batrulz 2011-02-25 20:23:42