2012-03-24 99 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(); 
    } 
}