2012-03-24 38 views
1

這是所需的行爲:讓用戶在運行時自定義用戶控件的內容屬性

我在畫布上有各種控件,標註(來自Expression Blend .dll)或簡單的標籤。當用戶「雙擊」(或者我決定綁定的任何其他事件)時,控件應該改變它的外觀以允許用戶編輯控件的內容屬性。然後單擊關閉控件,然後將其恢復爲「只讀」方法。

有關如何最好地實現這一點的任何建議?理想情況下,我希望在c#中完成這一切,以便在運行時將此行爲添加到控件中(因爲此控件將動態添加到畫布中) - 並完全避免使用XAML。

我認爲我必須做一些裝飾物來顯示綁定到控件的內容屬性上的必需事件的文本框,但一些代碼示例或鏈接在其他地方將不勝感激? :) - 我一直無法在現有搜索中找到任何內容,但我認爲它應該相當簡單。

回答

0

不幸的是,風格觸發器不會對IsReadOnly和IsEnabled做任何事情。你將不得不從事件中做到這一點。

這裏是我的示例:

WPF:

<Window x:Class="StateChangingTextbox.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 

    <Window.Resources> 
     <Style TargetType="TextBox"> 
      <Style.Triggers> 
       <Trigger Property="IsMouseOver" Value="True"> 
        <Setter Property="Background" Value="#eee" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 

    <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> 
     <TextBox Width="300" Height="200" TextWrapping="Wrap" IsReadOnly="True" 
      MouseEnter="TextBox_MouseEnter" 
      MouseLeave="TextBox_MouseLeave"/> 
    </Grid> 
</Window> 

代碼隱藏:

/// <summary> 
/// Interaction logic for MainWindow.xaml 
/// </summary> 
public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void TextBox_MouseEnter(object sender, MouseEventArgs e) 
    { 
     var textbox = sender as TextBox; 
     if (textbox != null) 
     { 
      textbox.IsReadOnly = false; 
     } 
    } 

    private void TextBox_MouseLeave(object sender, MouseEventArgs e) 
    { 
     var textbox = sender as TextBox; 
     if (textbox != null) 
     { 
      textbox.IsReadOnly = true; 
     } 
    } 
} 
+0

感謝您的代碼,它讓我走了。最後,我完全避免了Style觸發器,並創建了我自己的用戶控件,添加了激活和取消激活方法來更改控件的視圖。這種方法連接到控件的雙擊事件(激活),失去焦點(禁用)。我發佈了我的代碼,如果它可以幫助任何人。 – mistercormell 2012-03-31 14:02:40

0

XAML:

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" 
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
     xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
mc:Ignorable="d" 
x:Class="ComicWPF.Bubble" 
x:Name="UserControl" Height="100" Width="200"> 

<Canvas LostFocus="this_LostFocus"> 
    <ed:Callout x:Name="callout" Content="" 
     AnchorPoint="0,1" FontSize="14" Height="100" Width="200" 
     Fill="Blue" 
     PreviewMouseDoubleClick="Callout_DoubleClick" 
     Canvas.Left="0" Canvas.Top="0" /> 
    <TextBox x:Name="textbox" 
      FontSize="14" 
      Canvas.Left="30" Height="55" Width="80" Canvas.Top="30" 
      Visibility="Visible"/> 
</Canvas> 
</UserControl> 

C#代碼:

private void Callout_DoubleClick(object sender, MouseButtonEventArgs e) 
    { 
     Activate(); 
    } 

    public void Activate() 
    { 
       //set bool activated to true 
       //make textbox visible and set focus and select all text 
    } 

    private void Callout_DeSelect() 
    { 
      //set content of callout to the textbox.Text 
      //Hide textbox 
      //set bool activated to false 
    } 

    private void this_LostFocus(object sender, RoutedEventArgs e) 
    { 
     Callout_DeSelect(); 
    } 
} 
相關問題