2014-06-06 55 views
0

我有動態列表框包含文本框作爲列表框項綁定可觀察集合。默認情況下,這個文本框處於只讀模式,我想讓它在編輯按鈕點擊該列表框項目時可編輯。編輯按鈕僅當列表框項目上的鼠標懸停時纔可見。如何使編輯按鈕單擊文本框可編輯?如何更改ListBoxitem(這裏是文本框)ListBoxitem(這裏是按鈕)的點擊事件的屬性

這裏是我的嘗試:

<ListBox Name="ListUrl" ItemsSource="{Binding .,Source=CollectionUrl,BindsDirectlyToSource=True}"> 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Style.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="IsSelected" Value="True"/> 
         <Setter Property="Background" Value="Gray"/> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </ListBox.ItemContainerStyle> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Grid> 
        <Grid.ColumnDefinitions> 
         <ColumnDefinition Width="100" /> 
         <ColumnDefinition Width="30" /> 
         <ColumnDefinition Width="30"/> 
        </Grid.ColumnDefinitions> 
        <RadioButton> 
         <TextBox Name="TextBoxUrl" Text="{Binding Path=urlString, Mode=TwoWay}" Foreground="Gray",IsReadOnly= True> 
          <TextBox.Style> 
           <Style TargetType="TextBox"> 
            <Style.Triggers> 
             <DataTrigger Binding="{Binding ElementName=EditButton , Path=IsPressed}" Value="True"> 
              <Setter Property="IsReadOnly" Value="False"/> 
             </DataTrigger> 
            </Style.Triggers> 
           </Style> 
          </TextBox.Style> 
         </TextBox> 
        </RadioButton> 

        <Button Name="EditButton" Content="Edit" Grid.Column="1" Click="Button_Click"> 
         <Button.Style> 
          <Style TargetType="Button"> 
           <Setter Property="Visibility" Value="Collapsed" /> 
           <Style.Triggers> 
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}, AncestorLevel=1}, Path=IsSelected}" Value="True"> 
             <Setter Property="Visibility" Value="Visible" /> 
             <Setter Property="ClickMode" Value="Press"/> 
            </DataTrigger> 
           </Style.Triggers> 
          </Style> 
         </Button.Style> 
        </Button> 
        <Button Content="Delete" Grid.Column="2" Click="Button_Click_1" HorizontalAlignment="Right" Margin="0"> 
         <Button.Style> 
          <Style TargetType="Button"> 
           <Setter Property="Visibility" Value="Collapsed" /> 
           <Style.Triggers> 
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}, AncestorLevel=1}, Path=IsSelected}" Value="True"> 
             <Setter Property="Visibility" Value="Visible" /> 
             <Setter Property="ClickMode" Value="Press"/> 
            </DataTrigger> 
           </Style.Triggers> 
          </Style> 
         </Button.Style> 
        </Button>    
       </Grid> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

回答

0

相反的Button,用戶ToggleButton並創建包含urlString屬性類一個屬性bool IsEditing

然後,你可以綁定IsEditing既ToggleButton.IsCheckedTextBox.IsReadOnly

<ToggleButton IsChecked="{Binding IsEditing}"/> 

<TextBox Name="TextBoxUrl" IsReadOnly="{Binding IsEditing}"/> 

不必說,你的類應該實現INotifyPropertyChanged和調高IsEditing

屬性更改
相關問題