这个标签在不同的环境中意味着不同的东西;考虑使用较少模糊的标签来代替或另外。常见含义包括:依赖项注入和数据绑定到对象和应用程序组件之间的绑定。
从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>
我在我的页面上具有3列的控制。我需要做的是用各自的数据填充每一列。 我有一个定义“完整”的类,该课程由3个字符串(站点,距离和Gasprice)组成
Get-Service -Name winrm, netlogon
用跟踪栏和字段中的clast binding textbox在class
我在与TrackBar的绑定文本框以及类上的属性中绑定了问题。 我将textbox.text属性绑定到字段 “进度” 在我的对象中。接下来,我设法从轨道绑定了属性值...
我有一个对象人,它具有名称和姓氏属性。 在我的WPF UI中,我有一个标签,需要绑定到全名: 我不打算...
I具有可选字符串作为类型的绑定,在父视图中,如果条件检查其是否具有值。根据这种情况,我显示或隐藏了孩子的视图。 WH ...
在WPF中,如果我使用autogeneratecolumns并将其绑定到我的类对象集合中,它将枚举显示为组合obobox自动在其中我可以在枚举中的所有选项之间进行选择: <
<DataTemplate x:Key="JobListItem"> <ViewCell> <effectsView:SfEffectsView x:Name="JobListTouch" TouchDownEffects="Ripple" FadeOutRipple="True" RippleAnimationDuration="300"> <effectsView:SfEffectsView.Behaviors> <toolkit:EventToCommandBehavior EventName="AnimationCompleted" Command="{Binding Source={RelativeSource AncestorType={x:Type listView:SfListView}}, Path=ShowJobCommand}"/> </effectsView:SfEffectsView.Behaviors> </ViewCell> </DataTemplate
在常见的LISP中,您可以做到这一点: (defun foo(bar&key baz quux) (列表bar baz quux)) (foo 1:xuux 3:baz 2); =>(1 2 3) Clojure没有关键字参数。 另一种选择是: (
i我读了角度绑定对象属性的手册,但是我很难找到完美的最优化的解决方案。 假设我有模型用户 导出类用户{ 公共_id:数字; ...