code-behind 相关问题

代码隐藏是指包含在单独的类文件中的UI代码(Windows窗体,ASP.NET页面等)。这允许分离UI及其背后的逻辑。

使用C#添加wpf代码中的fontawesome图标到数据网格行

i有一个带有行的数据杂志,从编程中开始添加,而我的datagrid in xaml有5列,每个列样式如下所示: <DataGrid.Columns> <DataGridTextColumn Width="Auto" Header="Status" FontFamily="Arial" Foreground="Gray" FontWeight="Normal" MinWidth="250" Binding="{Binding Path=Date}" > <DataGridTextColumn.HeaderTemplate> <DataTemplate> <StackPanel> <fa:ImageAwesome Icon="Sort" Height="10" Name="Search" Margin="10,15,25,0" HorizontalAlignment="Center" /> <!--<TextBlock FontFamily="FontAwesome" FontSize="16" Text="&#xf0dc;" />--> <TextBlock Text="Date" Margin="0,-30,0,0" /> </StackPanel> </DataTemplate> </DataGridTextColumn.HeaderTemplate> <DataGridTextColumn.ElementStyle> <Style> <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/> <Setter Property="FrameworkElement.Margin" Value="0,8,0,0"></Setter> </Style> </DataGridTextColumn.ElementStyle> <DataGridTextColumn.HeaderStyle> <Style TargetType="{x:Type DataGridColumnHeader}"> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="Background" Value="#dec15a" /> <Setter Property="Height" Value="50" /> <Setter Property="FontWeight" Value="Bold" /> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="#d5b024" /> </Trigger> </Style.Triggers> </Style> </DataGridTextColumn.HeaderStyle> </DataGridTextColumn> </DataGrid.Columns> 如何在文本旁边添加字体上的成功图标,如下所示,当行从后面添加代码时添加 Date | File| Status 03/14/2025|.pdf | Success (here the FontAwesome success icon should be shown) Code-Behind代码为: dgrd_ListFiles.Items.Add(new Item() { Date = System.DateTime.Now.ToString("MM/dd /yyyy"), File=".pdf", Status = "Success" }); 我认为以下情况应在您的情况下起作用。修改如下:标签: DataGridTextColumn.ElementStyle

回答 1 投票 0

使用C#添加wpf代码中的wpf代码中的数据网格行添加了“真棒图标”

where是一个数据杂志,具有编程性添加行的行,而我的datagrid in XAML,带有5列,每个列样式均为下面 <DataGrid.Columns> <DataGridTextColumn Width="Auto" Header="Status" FontFamily="Arial" Foreground="Gray" FontWeight="Normal" MinWidth="250" Binding="{Binding Path=Date}" > <DataGridTextColumn.HeaderTemplate> <DataTemplate> <StackPanel> <fa:ImageAwesome Icon="Sort" Height="10" Name="Search" Margin="10,15,25,0" HorizontalAlignment="Center" /> <!--<TextBlock FontFamily="FontAwesome" FontSize="16" Text="&#xf0dc;" />--> <TextBlock Text="Date" Margin="0,-30,0,0" /> </StackPanel> </DataTemplate> </DataGridTextColumn.HeaderTemplate> <DataGridTextColumn.ElementStyle> <Style> <Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/> <Setter Property="FrameworkElement.Margin" Value="0,8,0,0"></Setter> </Style> </DataGridTextColumn.ElementStyle> <DataGridTextColumn.HeaderStyle> <Style TargetType="{x:Type DataGridColumnHeader}"> <Setter Property="HorizontalContentAlignment" Value="Center" /> <Setter Property="Background" Value="#dec15a" /> <Setter Property="Height" Value="50" /> <Setter Property="FontWeight" Value="Bold" /> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter Property="Background" Value="#d5b024" /> </Trigger> </Style.Triggers> </Style> </DataGridTextColumn.HeaderStyle> </DataGridTextColumn> </DataGrid.Columns> 如何在文本旁边添加字体上的成功图标,如下所示,当行从后面添加代码时添加 Date | File| Status 03/14/2025|.pdf | Success (Here should come the font awesone succes icon) 代码背后的编码是 dgrd_ListFiles.Items.Add(new Item() { Date = System.DateTime.Now.ToString("MM/dd /yyyy"), File=".pdf", Status = "Success" }); 我认为以下情况应在您的情况下起作用。修改如下:标签: DataGridTextColumn.ElementStyle

回答 1 投票 0

从usercontrol到window的代码behind databind 我有一个问题要问:我正在尝试绑定2个字符串,一个从用户控制到窗口的代码。 这是Usercontrol风格:

这是用户控件的代码文件: using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace PasswordManager.Helpers.Resource_UserDefined { public partial class TextBox_Hint : UserControl, INotifyPropertyChanged { public TextBox_Hint() { InitializeComponent(); } public string TextBase { get { return (string)GetValue(TextProperty); } set { SetValue(TextProperty, value); OnPropertyChanged(); } } // Using a DependencyProperty as the backing store for TextBase. This enables animation, styling, binding, etc... public static readonly DependencyProperty TextProperty = DependencyProperty.Register("TextBase", typeof(string), typeof(TextBox_Hint)); public string Hint { get { return (string)GetValue(HintProperty); } set { SetValue(HintProperty, value); } } // Using a DependencyProperty as the backing store for Hint. This enables animation, styling, binding, etc... public static readonly DependencyProperty HintProperty = DependencyProperty.Register("Hint", typeof(string), typeof(TextBox_Hint), new PropertyMetadata("")); public Brush Background { get { return (Brush)GetValue(BackgroundProperty); } set { SetValue(BackgroundProperty, value); } } // Using a DependencyProperty as the backing store for background. This enables animation, styling, binding, etc... public static readonly DependencyProperty BackgroundProperty = DependencyProperty.Register("Background", typeof(Brush), typeof(TextBox_Hint), new PropertyMetadata(Brushes.White)); public Brush BorderColorBrush { get { return (Brush)GetValue(BorderColorBrushProperty); } set { SetValue(BorderColorBrushProperty, value); } } // Using a DependencyProperty as the backing store for BorderColorBrush. This enables animation, styling, binding, etc... public static readonly DependencyProperty BorderColorBrushProperty = DependencyProperty.Register("BorderColorBrush", typeof(Brush), typeof(TextBox_Hint), new PropertyMetadata(Brushes.Black)); public Thickness BorderThickness { get { return (Thickness)GetValue(BorderThicknessProperty); } set { SetValue(BorderThicknessProperty, value); } } // Using a DependencyProperty as the backing store for BorderThickness. This enables animation, styling, binding, etc... public static readonly DependencyProperty BorderThicknessProperty = DependencyProperty.Register("BorderThickness", typeof(Thickness), typeof(TextBox_Hint), new PropertyMetadata(new Thickness(1,1,1,1))); public Brush ForegroundColorOverlay { get { return (Brush)GetValue(ForegroundColorOverlayProperty); } set { SetValue(ForegroundColorOverlayProperty, value); } } // Usando un DependencyProperty come memorizzazione per ForegroundColorOverlay public static readonly DependencyProperty ForegroundColorOverlayProperty = DependencyProperty.Register("ForegroundColorOverlay", typeof(Brush), typeof(TextBox_Hint), new PropertyMetadata(Brushes.Gray)); public FontFamily FontFamily { get { return (FontFamily)GetValue(FontFamilyProperty); } set { SetValue(FontFamilyProperty, value); } } // Using a DependencyProperty as the backing store for FontFamily. This enables animation, styling, binding, etc... public static readonly DependencyProperty FontFamilyProperty = DependencyProperty.Register("FontFamily", typeof(FontFamily), typeof(TextBox_Hint), new PropertyMetadata(new FontFamily("Arial"))); public FontStyle FontStyle { get { return (FontStyle)GetValue(FontStyleProperty); } set { SetValue(FontStyleProperty, value); } } // Using a DependencyProperty as the backing store for FontStyle. This enables animation, styling, binding, etc... public static readonly DependencyProperty FontStyleProperty = DependencyProperty.Register("FontStyle", typeof(FontStyle), typeof(TextBox_Hint), new PropertyMetadata(FontStyles.Normal)); public Double FontSize { get { return (Double)GetValue(FontSizeProperty); } set { SetValue(FontSizeProperty, value); } } // Using a DependencyProperty as the backing store for FontSize. This enables animation, styling, binding, etc... public static readonly DependencyProperty FontSizeProperty = DependencyProperty.Register("FontSize", typeof(Double), typeof(TextBox_Hint), new PropertyMetadata(20.0)); private void txtSearchBox_TextChanged(object sender, TextChangedEventArgs e) { if (string.IsNullOrEmpty(txtSearchBox.Text)) txt_HintBox.Visibility = Visibility.Visible; else txt_HintBox.Visibility = Visibility.Hidden; } public event PropertyChangedEventHandler? PropertyChanged; protected void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } 我使用这样的用户控件: <userControls:TextBox_Hint x:Name="TextBox_Reame" Height="40" BorderColorBrush="Black" BorderThickness="0,0,0,1" FontFamily="{DynamicResource Abel}" FontSize="20" FontStyle="Italic" ForegroundColorOverlay="Gray" Hint="Inserisci Il Reame" TextBase="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type userControls:TextBox_Hint}}, Path=TextBase}"> <userControls:TextBox_Hint.Background> <LinearGradientBrush StartPoint="0,0" EndPoint="1,0"> <GradientStop Offset="0.0" Color="White" /> <GradientStop Offset="0.5" Color="#FFE1FFE6" /> <GradientStop Offset="1.0" Color="White" /> </LinearGradientBrush> </userControls:TextBox_Hint.Background> </userControls:TextBox_Hint> 这是字符串属性: private string _Reame; public string Reame { get { return _Reame; } set { _Reame = value; OnPropertyChanged(); } } 现在问题开始了。我已经尝试了1000次尝试解决问题,但没有任何回应。我认为我的问题很简单...如何将(用户控件的)文本键值直接获取到我使用的窗口的代码范围内? 如果我使用TextBox_Reame.TextBase,我可以完美地获得值,但是我想使用绑定。我对此窗口的视图模型不感兴趣,因为数据值低。 感谢所有人 我将支持@clemens。我也不完全了解您想做什么,什么对您不起作用。 我将假设您在数据上下文中具有带有“公共字符串reame”属性的ViewModel。 在这种情况下,应将“ textbox_reame.textbase”绑定设置为此属性,而不是自身。 <userControls:TextBox_Hint x:Name="TextBox_Reame" Height="40" BorderColorBrush="Black" BorderThickness="0,0,0,1" FontFamily="{DynamicResource Abel}" FontSize="20" FontStyle="Italic" ForegroundColorOverlay="Gray" Hint="Inserisci Il Reame" TextBase="{Binding Reame}"> <!--TextBase="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type userControls:TextBox_Hint}}, Path=TextBase}">--> <userControls:TextBox_Hint.Background> <LinearGradientBrush StartPoint="0,0" EndPoint="1,0"> <GradientStop Offset="0.0" Color="White" /> <GradientStop Offset="0.5" Color="#FFE1FFE6" /> <GradientStop Offset="1.0" Color="White" /> </LinearGradientBrush> </userControls:TextBox_Hint.Background> </userControls:TextBox_Hint> 我还建议您使用自定义控件而不是UserControl: using System.Windows; using System.Windows.Controls; using System.Windows.Media; namespace SOTopics2025.SO.user27828246.question79511053 { public class TextBox_Hint : TextBox { static TextBox_Hint() { DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBox_Hint), new FrameworkPropertyMetadata(typeof(TextBox_Hint))); } public string Hint { get => (string)GetValue(HintProperty); set => SetValue(HintProperty, value); } // Using a DependencyProperty as the backing store for Hint. This enables animation, styling, binding, etc... public static readonly DependencyProperty HintProperty = DependencyProperty.Register(nameof(Hint), typeof(string), typeof(TextBox_Hint), new PropertyMetadata(string.Empty)); public Brush HintForeground { get => (Brush)GetValue(HintForegroundProperty); set => SetValue(HintForegroundProperty, value); } // Usando un DependencyProperty come memorizzazione per ForegroundColorOverlay public static readonly DependencyProperty HintForegroundProperty = DependencyProperty.Register(nameof(HintForeground), typeof(Brush), typeof(TextBox_Hint), new PropertyMetadata(Brushes.Gray)); public Visibility HintVisibility { get => (Visibility)GetValue(HintVisibilityProperty); private set => SetValue(HintVisibilityPropertyKey, value); } // Using a DependencyProperty as the backing store for Hint. This enables animation, styling, binding, etc... public static readonly DependencyPropertyKey HintVisibilityPropertyKey = DependencyProperty.RegisterReadOnly(nameof(HintVisibility), typeof(Visibility), typeof(TextBox_Hint), new PropertyMetadata(Visibility.Visible)); public static readonly DependencyProperty HintVisibilityProperty = HintVisibilityPropertyKey.DependencyProperty; protected override void OnTextChanged(TextChangedEventArgs e) { base.OnTextChanged(e); HintVisibility = string.IsNullOrEmpty(Text) ? Visibility.Visible : Visibility.Collapsed; } } } themes/generic.xaml: <SolidColorBrush x:Key="TextBox_Hint.Static.Border" Color="#FFABAdB3"/> <SolidColorBrush x:Key="TextBox_Hint.MouseOver.Border" Color="#FF7EB4EA"/> <SolidColorBrush x:Key="TextBox_Hint.Focus.Border" Color="#FF569DE5"/> <Style TargetType="{x:Type custcont:TextBox_Hint}"> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> <Setter Property="BorderBrush" Value="{StaticResource TextBox_Hint.Static.Border}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/> <Setter Property="HorizontalContentAlignment" Value="Left"/> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Setter Property="AllowDrop" Value="true"/> <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type custcont:TextBox_Hint}"> <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True"> <Grid> <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/> <TextBlock x:Name="Hint_text" Text="{TemplateBinding Hint}" Foreground="{TemplateBinding HintForeground}" Visibility="{TemplateBinding HintVisibility}" IsHitTestVisible="False"/> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Opacity" TargetName="border" Value="0.56"/> </Trigger> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox_Hint.MouseOver.Border}"/> </Trigger> <Trigger Property="IsKeyboardFocused" Value="true"> <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox_Hint.Focus.Border}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/> <Condition Property="IsSelectionActive" Value="false"/> </MultiTrigger.Conditions> <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/> </MultiTrigger> </Style.Triggers> </Style> usage: <StackPanel> <FrameworkElement.DataContext> <local:SomeViewModel Reame="Some text"/> </FrameworkElement.DataContext> <local:TextBox_Hint Hint="Inserisci Il Reame" Text="{Binding Reame, UpdateSourceTrigger=PropertyChanged}" Margin="10"/> <TextBlock Text="{Binding Reame}"/> </StackPanel>

回答 1 投票 0


如何指定数据台面是任何给定XAML的代码模块? (转换“ avalonia combobox”示例)

追求另一个初学者问题。 我正在尝试将Avalonia文档中的示例包括在我的测试项目中,但我一直坚持使用MainWindowViewModel以及Code-B ...

回答 0 投票 0

如何正确设置 razor 页面以访问代码隐藏中的数据

我是 Razor Pages 新手,我发现可以使用代码隐藏文件将代码分为两部分。因此,我创建了这个 Index 类来初始化我的日期并将其打印到 .cshtml 文件中。但是当...

回答 2 投票 0

将 javascript onclick 添加到单选按钮 INPUT 标签

我有一个javascript函数,需要将其添加到一些动态创建的单选按钮onclick中的代码隐藏中。 我已经尝试过 - newRadioSelect.Attributes.Add("onlick", "javascript:toggle(this);")...

回答 4 投票 0

Visual Studio 消息:文档项没有代码隐藏文件。 (为 Powershell 构建 GUI)

在 Visual Studio 中,我创建了一个新的 PowerShell 脚本项目来为脚本构建 GUI。现在,了解其工作原理的概念很简单。 在 GUI 中我想...

回答 2 投票 0

Javascript 中 PageMethods 成功函数返回结果

我成功地从我的 C# 代码后面调用了 Javascript 中的 PageMethod,如下所示。 PageMethods.getTextResume(sProfileID, 函数(结果){警报(结果);}, 函数(结果){警报(结果);}); ...

回答 3 投票 0

清除页面上的所有文本框和下拉控件

在用户选择“提交”按钮后,我试图清除/重置页面上的所有文本框和下拉控件。 我想以编程方式执行此操作,因此如果添加/删除其他控件

回答 1 投票 0

无代码隐藏的 ASP.net 页面

我有一个 ASP.Net 页面,后面有 C# 代码。 但是,我被要求不要使用隐藏代码,以便更容易在 SharePoint 中部署。 有没有办法将 C# 代码包含在...

回答 6 投票 0

在 Page_PreInit 和 Page_Load 之间传递对象/变量

我正在使用数据库来存储页面信息并使用 .NET (C#) 来动态构建页面来构建页面。 该过程的一部分是根据数据中的内容设置母版页(在代码隐藏中)...

回答 4 投票 0

WPF MVVM 代码背后的最佳实践

我是一名使用 MVVM 模式通过 WPF 学习 C# 的学生。最近,我一直在研究我的应用程序的艺术(自定义启动屏幕),当我不希望它关闭时,它不应该关闭。 我有...

回答 3 投票 0

如何修复任务栏中出现的状态按钮

问题:我试图使状态按钮出现在相应的列中,但失败了。 桌子的图像 采取的步骤: 只是为了让您知道,任务列是手动添加的,这是...

回答 1 投票 0

视图模型文件中的我的 ObservableCollection<T> 无法被其他文件看到

我在名为 ViewModel 的文件夹中有一个视图模型类。该文件名为 PgViewMode.cs,如下所示: 使用 CommunityToolkit.Mvvm.ComponentModel; 使用 LockAndKeyMaui.Models; 使用

回答 1 投票 0

如何从视图模型类设置代码隐藏属性?

我从我的内容页面调用视图模型类中的方法,如下所示: 命令=“{绑定GetDbsCommand}” 通过这个方法,我希望它在我的代码隐藏中设置一个属性...

回答 1 投票 0

将块提取到代码隐藏在 .NET 8 和 Visual Studio 2022 中不再可用

我在 Visual Studio 2022 中使用 .NET 8 和 Blazor,我想将我的 C# 代码移至代码隐藏文件。如果您高度重视,应该有一个名为“提取块到代码后面”的选项...

回答 1 投票 0

如何更改窗口的大小?

我从大小为 1285 x 800 的初始内容页面开始。我使用 shell 导航,并说 Shell.Current.Navigation.PushModalAsync(new NewDb()); 在 NewDb 内容页面中我有: 保护...

回答 1 投票 0

我在将一个控件绑定到另一个控件时遇到问题。为什么?

在我的 XAML 文件中,内容页面标签如下所示: 在我的 XAML 文件中,内容页面标签如下所示: <ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui" xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:viewmodel="clr-namespace:Tester.ViewModel" x:DataType="viewmodel:Viewmodel" x:Class="Tester.View.NewDb" BackgroundColor="DarkGreen"> <ContentPage.Resources> <toolkit:InvertedBoolConverter x:Key="InvertedBoolConverter" /> </ContentPage.Resources> 所以我有一个名为 Viewmodel 的类,用于数据绑定。 我正在尝试将输入控件绑定到复选框控件,如下所示: <Entry Placeholder="Enter Database Password" Grid.Row="2" Margin="20,0,20,0" TextColor="White" x:Name="dbPasswd" BindingContext="{x:Reference chkShowPasswd}" IsPassword="{Binding IsChecked, Converter={StaticResource InvertedBoolConverter}}"/> <HorizontalStackLayout Grid.Row="2" Grid.Column="1" HorizontalOptions="Center"> <CheckBox x:Name="chkShowPasswd" VerticalOptions="Center"/> <Label Text="Show Password" TextColor="White" FontSize="Micro" VerticalOptions="Center"/> </HorizontalStackLayout> 当我运行该程序时,我收到一条错误消息: 绑定:在 Tester.ViewModel.Viewmodel.cs 上找不到属性“IsChecked” 现在我在这里有更多的入口控件,我希望将它们绑定到 Viewmodel 代码隐藏中的 ObservableObjects 以及其他任务和方法。 所以我的问题是,如何绑定我在视图模型中显示的两个控件? 在绑定源中指定对复选框的引用: <Entry x:Name="dbPasswd" Grid.Row="2" Margin="20,0,20,0" IsPassword="{Binding IsChecked, Source={x:Reference chkShowPasswd}, Converter={StaticResource InvertedBoolConverter}}" Placeholder="Enter Database Password" TextColor="White" />

回答 1 投票 0

如何在代码隐藏中使用WPF的星号大小值?

我在代码隐藏中创建了一些控件,并希望动态设置其大小。 我可以分配数值以及 System.Windows.GridLength.Auto,但没有相当于“*”的值。 那是...

回答 1 投票 0

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.