让用户在运行时自定义用户控件的内容属性

时间:2012-03-24 13:42:47

标签: c# wpf wpf-controls expression-blend

这是必需的行为:

我在画布上有各种控件,例如标注(来自Expression Blend .dll)或简单标签。当用户“双击”(或我决定绑定的任何其他事件)时,控件应更改其外观以允许用户编辑控件的Content属性。单击控件然后应将其恢复为“只读”方法。

有关如何最好地实现这一目标的任何建议?理想情况下,我想在c#中全部执行此操作,以便在运行时将此行为添加到控件中(因为此控件已动态添加到画布中) - 并完全避免使用XAML。

我认为我必须与装饰师做一些事情来显示绑定到控件的内容属性上的文本框所需的事件,但是其他地方的一些代码示例或链接会受到赞赏吗? :) - 我无法在现有搜索中找到任何内容,但我认为它应该相当简单。

2 个答案:

答案 0 :(得分: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;
        }
    }
}

答案 1 :(得分: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();
    }
}