动态分配图像到WPF中的文本框模板

安基·坎多里亚

我已经为文本框实现了一个模板,以在其中显示图像,但是我面临的问题是我无法动态显示图像。这是我的代码

Window.xaml

<TextBox Style="{StaticResource imageTextBox}" />

这是我的文本框模板

<Style TargetType="Control" x:Key="imageTextBox">
    <Setter Property="Margin" Value="0,0,0,8"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Control">
                <Border x:Name="bg" BorderThickness="1" CornerRadius="3" BorderBrush="Gray">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="30"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <TextBox Style="{DynamicResource BasicTextBox}" Grid.Column="1" 
                                 Foreground="{TemplateBinding Foreground}" 
                                 Background="{TemplateBinding Background}" 
                                 FontFamily="{TemplateBinding FontFamily}"
                                 FontSize="{TemplateBinding FontSize}"
                                 FontWeight="{TemplateBinding FontWeight}"
                                 MinWidth="340" Margin="1" />
                        <Image Source="Images/img_1.gif" Width="20"/>
                    </Grid>
                </Border>

                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="BorderBrush" TargetName="bg" Value="Black"/>
                    </Trigger>
                    <Trigger Property="IsFocused" Value="True">
                        <Setter Property="BorderBrush" TargetName="bg" Value="Black"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style x:Key="BasicTextBox" TargetType="{x:Type TextBox}">
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="BorderThickness" Value="0"/>
</Style>
代码改善

您可以将{Binding}用作<Image.Source>属性。这是一个作为LinqPad查询示例免费获取LinqPad):

void Main()
{
    // Need to parse XAML since LinqPad doesn't have an integrated XAML build
    var xaml = @"
    <Grid xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"" xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
    <Grid.Resources>
    <Style TargetType=""Control"" x:Key=""imageTextBox"">
        <Setter Property=""Margin"" Value=""0,0,0,8""/>
        <Setter Property=""Template"">
            <Setter.Value>
                <ControlTemplate TargetType=""Control"">
                    <Border x:Name=""bg"" BorderThickness=""1"" CornerRadius=""3"" BorderBrush=""Gray"">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width=""30""/>
                                <ColumnDefinition Width=""*""/>
                            </Grid.ColumnDefinitions>
                            <TextBox Style=""{DynamicResource BasicTextBox}"" Grid.Column=""1""
                                     Foreground=""{TemplateBinding Foreground}""
                                     Background=""{TemplateBinding Background}""
                                     FontFamily=""{TemplateBinding FontFamily}""
                                     FontSize=""{TemplateBinding FontSize}""
                                     FontWeight=""{TemplateBinding FontWeight}""
                                     MinWidth=""340"" Margin=""1"" />
                            <Image Source=""{Binding Image}"" Width=""20""/>
                        </Grid>
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property=""IsMouseOver"" Value=""True"">
                            <Setter Property=""BorderBrush"" TargetName=""bg"" Value=""Black""/>
                        </Trigger>
                        <Trigger Property=""IsFocused"" Value=""True"">
                            <Setter Property=""BorderBrush"" TargetName=""bg"" Value=""Black""/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    </Grid.Resources>
    <TextBox Style=""{StaticResource imageTextBox}"" />
    </Grid>
    ";

    // Get some images... these could be in your resources
    var png = PngBitmapDecoder.Create(new Uri("https://upload.wikimedia.org/wikipedia/commons/9/97/Esperanto_star.png"), BitmapCreateOptions.None, BitmapCacheOption.Default);
    var vm = new ViewModel { Image = png.Frames[0] };
    var o = (FrameworkElement)XamlReader.Parse(xaml);
    // Set the image source - in this case, a view model
    o.DataContext = vm;
    // Let LinqPad display it
    PanelManager.DisplayWpfElement(o);

    // This code is for the example only, to change the image after 2 seconds.
    var dispatcher = o.Dispatcher;
    Task.Run(async () =>
    {
        await Task.Delay(2000);
        await dispatcher.BeginInvoke((Action)(() =>
        {
            png = PngBitmapDecoder.Create(new Uri("https://upload.wikimedia.org/wikipedia/commons/f/f6/Lol_question_mark.png"), BitmapCreateOptions.None, BitmapCacheOption.Default);
            vm.Image = png.Frames[0];
        }));
    });
}

// Define other methods and classes here
public class ViewModel : INotifyPropertyChanged
{
    private ImageSource _image;
    public event PropertyChangedEventHandler PropertyChanged;

    public ImageSource Image
    {
        get
        {
            return _image;
        }
        set
        {
            if (_image == value)
            {
                return;
            }

            _image = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Image)));
        }
    }
}

本示例使用一个简单的视图模型作为Binding源,但是它可以是任何源。还有一些代码会在Image2秒钟的延迟后更改该值,以向您显示它确实在更新。在您的代码中,您将忽略它并使用您想要更新它的方式。

首先,它显示为: 图像的首次出现

然后在更新后: 更新外观后

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

动态分配图像到WPF中的文本框模板

来自分类Dev

如何在Access中为VBA中的顺序命名文本框动态分配变量

来自分类Dev

在WPF中动态添加文本框

来自分类Dev

在颤动中动态分配图像

来自分类Dev

动态将文本框插入到用户控件中

来自分类Dev

清除绑定到wpf中的静态属性的文本框

来自分类Dev

将文本框值绑定到WPF中的模型

来自分类Dev

在WPF中以编程方式设置来自动态组合框的动态文本框的数据绑定

来自分类Dev

WPF按钮内容到文本框

来自分类Dev

从文本框中动态获取文本

来自分类Dev

从文件中读取数字到动态分配的数组中

来自分类Dev

Adobe Acrobat中的模板文本框

来自分类Dev

在页面中的每个图像弹出窗口上添加动态文本框以进行图像描述

来自分类Dev

在jQuery中动态添加文本框

来自分类Dev

在HTML中动态添加文本框

来自分类Dev

在PowerPoint中动态填充文本框

来自分类Dev

Shiny 中的动态文本框列表

来自分类Dev

在WPF中绑定文本框

来自分类Dev

ListView WPF中的文本框

来自分类Dev

在DataGrid WPF中查找文本框

来自分类Dev

在 WPF 中更改文本框颜色

来自分类Dev

WPF中具有扁平边框的单行文本框模板

来自分类Dev

在文本框css中添加图像

来自分类Dev

如何为文本框分配动态占位符?

来自分类Dev

WPF级联组合框到文本框

来自分类Dev

结构中的动态分配

来自分类Dev

如何删除在WPF网格中动态创建的文本框控件?

来自分类Dev

如何从Kendo网格模板文本框中获取值到角度数组

来自分类Dev

WPF中带有图像图标的文本框