WPF中的Binding的常见知识点与技巧

完全来源于十月的寒流,感谢大佬讲解
在这里插入图片描述

在XAML中,可以绑定到许多不同类型的数据源和属性。以下是一些可以绑定的常见数据源和属性:

  1. 属性:可以绑定到对象的属性,例如控件的TextVisibilityIsEnabled等属性。

  2. 集合:可以绑定到集合数据,如ListObservableCollectionArray等。在绑定到集合时,还可以使用索引器绑定到特定项。

  3. 静态资源:可以使用x:Static引用静态字段或属性,如常量、枚举、静态类的属性等。

  4. 数据上下文:在WPF和其他XAML框架中,每个元素都有一个数据上下文,可以在此上下文中绑定到其父元素的属性或继承的数据上下文的属性。

  5. 数据模型:可以绑定到MVVM(Model-View-ViewModel)模式中的数据模型,通常是一个实现INotifyPropertyChanged接口的类。

  6. XML和JSON数据:可以绑定到XML或JSON数据,使用XMLDataProvider或ObjectDataProvider等。

  7. 资源字典:可以绑定到资源字典中的资源,如样式、模板、图像等。

  8. 命令:可以使用Command绑定到自定义命令,以在用户交互时执行操作。

  9. 视觉状态:可以绑定到不同的视觉状态,以根据应用程序的当前状态更改UI。

  10. 动画:可以绑定到动画属性,以在动画执行时更改UI元素的属性。

这些只是一些常见的绑定数据源,实际上,XAML绑定是非常灵活的,可以将其用于几乎任何具有属性或数据的地方。数据绑定是XAML中非常强大的特性,可以用于创建动态、交互式和可扩展的用户界面。

一、Source

<Window x:Class="BindingTest.MainWindow"
        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:local="clr-namespace:BindingTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Window.DataContext>
        <local:MainWindowViewModel></local:MainWindowViewModel>
    </Window.DataContext>

    <Window.Resources>
    </Window.Resources>

    <StackPanel>
        <TextBlock Text="{Binding}" FontSize="30"></TextBlock>
    </StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
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 BindingTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }

    public class MainWindowViewModel
    {
        public string Message => "this is test";

        public override string ToString()
        {
            return "hello world"; 
        }
    }
}

后台实现binding

<Window x:Class="BindingTest.MainWindow"
        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:local="clr-namespace:BindingTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">

    <Window.DataContext>
        <local:MainWindowViewModel></local:MainWindowViewModel>
    </Window.DataContext>

    <Window.Resources>
    </Window.Resources>

    <StackPanel>
        <TextBlock x:Name="tbl" FontSize="30"></TextBlock>
    </StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
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 BindingTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            var binding = new Binding
            {
                Path = new PropertyPath("Message"),
                Mode = BindingMode.TwoWay,
            };
            BindingOperations.SetBinding(tbl, TextBlock.TextProperty, binding);
        }
    }

    public class MainWindowViewModel
    {
        private string message = "this is test";
        public string Message
        {
            get { return message; }
            set { message = value; }
        }
    }
}

绑定StaticResource资源

<Window x:Class="BindingTest.MainWindow"
        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:local="clr-namespace:BindingTest" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">

    <Window.DataContext>
        <local:MainWindowViewModel></local:MainWindowViewModel>
    </Window.DataContext>

    <Window.Resources>
        <sys:String x:Key="str">Hello, World</sys:String>
    </Window.Resources>

    <StackPanel>
        <TextBlock x:Name="tbl" FontSize="30" Text="{Binding Source={StaticResource str}}"></TextBlock>
    </StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
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 BindingTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }
    }

    public class MainWindowViewModel
    {
        private string message = "this is test";
        public string Message
        {
            get { return message; }
            set { message = value; }
        }
    }
}

使用StaticResource和DynamicResource资源

<Window x:Class="BindingTest.MainWindow"
        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:local="clr-namespace:BindingTest" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">

    <Window.DataContext>
        <local:MainWindowViewModel></local:MainWindowViewModel>
    </Window.DataContext>

    <Window.Resources>
        <sys:String x:Key="str">Hello, World</sys:String>
    </Window.Resources>

    <StackPanel>
        <!--<TextBlock x:Name="tbl_1" FontSize="30" Text="{Binding Source={DynamicResource str}}"></TextBlock>-->
        <TextBlock x:Name="tbl_2" FontSize="30" Text="{Binding Source={StaticResource str}}"></TextBlock>
        <TextBlock x:Name="tbl_3" FontSize="30" Text="{DynamicResource str}"></TextBlock>        
    </StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
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 BindingTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Resources["str"] = "GoodBye";
        }
    }
    
    public class MainWindowViewModel
    {
        private string message = "this is test";
        public string Message
        {
            get { return message; }
            set { message = value; }
        }
    }
}

       这是尝试在TextBlock元素的Source属性上使用DynamicResource,但是Source属性不是DependencyProperty,因此无法直接使用DynamicResource。只能在DependencyObject的DependencyProperty上使用DynamicResource。

<TextBlock x:Name="tbl_1" FontSize="30" Text="{Binding Source={DynamicResource str}}"></TextBlock>

       要在TextBlock中使用DynamicResource,应该将DynamicResource绑定到Text属性,而不是Source属性。例如,可以这样修改XAML:

<TextBlock x:Name="tbl_1" FontSize="30" Text="{DynamicResource str}"></TextBlock>

       这将允许使用DynamicResource来绑定Text属性,而不会引发异常。DynamicResource通常用于将资源动态应用到具有DependencyProperty的元素,而不是Source属性。

使用属性、静态属性、常量资源

<Window x:Class="BindingTest.MainWindow"
        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:local="clr-namespace:BindingTest" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">

    <Window.DataContext>
        <local:MainWindowViewModel></local:MainWindowViewModel>
    </Window.DataContext>

    <Window.Resources>
        <sys:String x:Key="str">Hello, World</sys:String>
        <local:MyResource x:Key="myres"></local:MyResource>
    </Window.Resources>

    <StackPanel>
        <!--<TextBlock x:Name="tbl_1" FontSize="30" Text="{Binding Source={DynamicResource str}}"></TextBlock>-->
        <!--<TextBlock x:Name="tbl_2" FontSize="30" Text="{Binding Source={StaticResource str}}"></TextBlock>-->
        <!--<TextBlock x:Name="tbl_3" FontSize="30" Text="{DynamicResource str}"></TextBlock>-->

        <TextBlock FontSize="30" Text="{Binding Source={StaticResource myres}, Path=Message}"></TextBlock>
        <TextBlock FontSize="30" Text="{Binding Source={x:Static local:MyResource.StaticString}}"></TextBlock>
        <TextBlock FontSize="30" Text="{Binding Source={x:Static local:MyResource.ConstString}}"></TextBlock>
    </StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
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 BindingTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.Resources["str"] = "GoodBye";
        }
    }

    public class MyResource
    {
        public string Message { get; } = "Public Property";
        public static string StaticString { get; } = "Static String";
        public const string ConstString = "Const String";
    }

    public class MainWindowViewModel
    {
        private string message = "this is test";
        public string Message
        {
            get { return message; }
            set { message = value; }
        }
    }
}

待学习
<CollectionViewSource></CollectionViewSource>
<ObjectDataProvider></ObjectDataProvider>

二、ElementName

<Window x:Class="BindingTest.MainWindow"
        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:local="clr-namespace:BindingTest" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">

    <Window.DataContext>
        <local:MainWindowViewModel></local:MainWindowViewModel>
    </Window.DataContext>

    <Window.Resources>
    </Window.Resources>

    <StackPanel>
        <TextBox FontSize="30" x:Name="txt"></TextBox>
        <TextBox FontSize="30" Text="{Binding ElementName=txt, Path=Text, Mode=TwoWay}"></TextBox>
    </StackPanel>
</Window>

在上述XAML代码中,第二个试图通过绑定将其文本与第一个TextBox的文本同步,而且将绑定模式设置为TwoWay。理论上,这意味着当更改第二个TextBox中的文本时,第一个TextBox的文本也应该相应地更改。

然而,在这里遇到的问题可能是由于两个TextBox之间的绑定路径的问题。具体来说,第二个TextBox的绑定路径ElementName=txt, Path=Text指示它应该与txt元素的Text属性进行双向绑定。这意味着它将复制txt元素的Text属性的值,但并不会与txtText属性绑定在一起,所以当更改第二个TextBox的文本时,第一个TextBox的文本不会随之更改。

如果想要实现两个TextBox之间的文本同步,可以尝试以下修改:

<StackPanel>
    <TextBox FontSize="30" x:Name="txt"></TextBox>
    <TextBox FontSize="30" Text="{Binding ElementName=txt, Path=Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></TextBox>
</StackPanel>

通过添加UpdateSourceTrigger=PropertyChanged,可以确保当第二个TextBox的文本更改时,立即将值传播回数据源(即第一个TextBoxText属性),从而实现双向绑定。这样,当更改第二个TextBox的文本时,第一个TextBox的文本也会跟着变化。

UpdateSourceTrigger介绍
UpdateSourceTrigger 是一个在数据绑定中用于指定何时更新数据源的属性。它通常用于WPF(Windows Presentation Foundation)、Windows Forms 和其他.NET应用程序中,用于将用户界面(UI)元素与数据源绑定起来。

以下是 UpdateSourceTrigger 的四个枚举值及其含义:

  1. Default:

    • 这是默认选项,通常情况下不需要显式设置。其行为取决于数据绑定上下文。
    • 通常,在大多数情况下,它的行为类似于 PropertyChanged,即当绑定的属性的值发生更改时立即更新数据源。
  2. PropertyChanged:

    • 当绑定的属性的值发生更改时,立即更新数据源。
    • 这是最常见的选项,特别是对于实时反馈或实时验证非常有用,因为每次属性值变化时都会立即更新数据源。
  3. LostFocus:

    • 数据源会在 UI 元素失去焦点(例如,用户离开输入框)时更新。
    • 这在需要减少数据源更新频率以提高性能的情况下可能很有用,因为它允许用户在输入数据之后再进行更新。
  4. Explicit:

    • 数据源只会在显式调用更新操作时进行更新。这通常需要通过编程来触发数据源的更新,而不是依赖于自动的值更改或 UI 元素失去焦点。
    • 这个选项适用于需要精确控制何时进行数据源更新的情况,可能需要在用户操作之后执行自定义逻辑。

异同点:

  • PropertyChangedLostFocus 会在特定的条件下自动触发数据源更新,分别是属性值更改和 UI 元素失去焦点。而 Explicit 需要手动触发更新。
  • Default 是一个根据上下文自动确定更新时机的选项,通常情况下表现为 PropertyChanged,但可以根据数据绑定上下文的不同而有所不同。

PopupRoot嵌套显示—代码有错误

<Window x:Class="Test_06.MainWindow"
        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:local="clr-namespace:Test_06" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" >

    <Window.Resources>
    </Window.Resources>

    <StackPanel x:Name="panel" Margin="10">
        <TextBox x:Name="txt" FontSize="30"></TextBox>
        <TextBlock Text="{Binding ElementName=txt, Path=Text}" FontSize="30">
            <TextBlock.ToolTip>
                <TextBlock Text="{Binding ElementName=txt, Path=Text}" FontSize="30"></TextBlock>
            </TextBlock.ToolTip>
        </TextBlock>
    </StackPanel>
</Window>

PopupRoot嵌套显示—代码修改正确

<Window x:Class="Test_06.MainWindow"
        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:local="clr-namespace:Test_06" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" >

    <Window.Resources>
    </Window.Resources>

    <StackPanel x:Name="panel" Margin="10">
        <TextBox x:Name="txt" FontSize="30"></TextBox>
        <TextBlock Text="{Binding ElementName=txt, Path=Text}" FontSize="30">
            <TextBlock.ToolTip>
                <TextBlock Text="{Binding Source={x:Reference Name=txt}, Path=Text}" FontSize="30"></TextBlock>
            </TextBlock.ToolTip>
        </TextBlock>
        <DataGrid>
            <DataGrid.Columns>
            <!--<DataGridTextColumn Header="{Binding ElementName=txt, Path=Text}" FontSize="30"></DataGridTextColumn>-->
                <DataGridTextColumn Header="{Binding Source={x:Reference Name=txt}, Path=Text}" FontSize="30"></DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </StackPanel>
</Window>

在WPF(Windows Presentation Foundation)中,SourceElementName 都用于数据绑定表达式,但有不同的用途。

  1. ElementName

    • ElementName 用于绑定到 XAML 标记中的另一个元素。指定要绑定到的元素的名称,然后引用其属性或数据上下文。
    • 例如,Header="{Binding ElementName=txt, Path=Text}" 表示正在将 Header 属性绑定到名称为 “txt” 的元素的 Text 属性。
  2. Sourcex:Reference

    • Source 用于在绑定表达式中直接指定数据的来源。在这种情况下,您可以使用 x:Reference 标记扩展来通过其 x:Name 引用另一个元素。
    • 例如,Header="{Binding Source={x:Reference txt}, Path=Text}" 表示您正在将 Header 属性绑定到具有 x:Name 为 “txt” 的元素的 Text 属性。

关键区别在于,ElementName 依赖于元素的 Name 属性来引用同一视觉树中的元素,而 Sourcex:Reference 直接通过 x:Name 引用元素。使用 x:Reference 可以在元素没有设置 Name 属性的情况下引用它,或者用于引用在当前视觉树之外定义的元素(例如,在不同资源字典中定义或在当前控件范围之外定义的元素)。

两者之间的区别总结:

  • ElementName 使用元素的 Name 属性在同一视觉树内引用元素。
  • Sourcex:Reference 使用元素的 x:Name 属性引用元素,可以用于位于当前视觉树之外或没有 Name 属性的元素。

RelativeSource绑定父级

<Window x:Class="Test_06.MainWindow"
        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:local="clr-namespace:Test_06" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" >

    <Window.Resources>
    </Window.Resources>

    <StackPanel x:Name="panel" Margin="10">
        <Grid Tag="Level 3">
            <Grid Tag="Level 2">
                <Grid Tag="Level 1">
                    <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid, AncestorLevel=1}, Path=Tag}" FontSize="30"></TextBlock>
                </Grid>
            </Grid>
        </Grid>
    </StackPanel>
</Window>
<Window x:Class="Test_06.MainWindow"
        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:local="clr-namespace:Test_06" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" >

    <Window.Resources>
    </Window.Resources>

    <StackPanel x:Name="panel" Margin="10">
        <TextBox x:Name="str" FontSize="30"></TextBox>
        <!--<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid, AncestorLevel=1}, Path=Tag}" FontSize="30"></TextBlock>-->
        <TextBlock FontSize="30" Text="{Binding RelativeSource={RelativeSource AncestorType=StackPanel}, Path=Children[0].Text}"></TextBlock>
    </StackPanel>
</Window>

在这里插入图片描述

绑定自己

方法一

<Window x:Class="BindingTest.MainWindow"
        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:local="clr-namespace:BindingTest" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" >

    <Window.Resources>
    </Window.Resources>

    <StackPanel x:Name="panel" Margin="10" RenderTransformOrigin="0.5,0.5">
        <TextBox x:Name="str" FontSize="30"></TextBox>
        <!--<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid, AncestorLevel=1}, Path=Tag}" FontSize="30"></TextBlock>-->
        <TextBlock FontSize="30" Text="{Binding RelativeSource={RelativeSource Mode=Self}}"></TextBlock>
    </StackPanel>
</Window>

方法二

<Window x:Class="BindingTest.MainWindow"
        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:local="clr-namespace:BindingTest" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" >

    <Window.Resources>
    </Window.Resources>

    <StackPanel x:Name="panel" Margin="10" RenderTransformOrigin="0.5,0.5">
        <TextBox x:Name="str" FontSize="30"></TextBox>
        <!--<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid, AncestorLevel=1}, Path=Tag}" FontSize="30"></TextBlock>-->
        <TextBlock FontSize="30" Text="{Binding RelativeSource={RelativeSource Self}, Path=ActualWidth}"></TextBlock>
    </StackPanel>
</Window>

方法三

<Window x:Class="BindingTest.MainWindow"
        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:local="clr-namespace:BindingTest" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" >

    <Window.Resources>
    </Window.Resources>

    <StackPanel x:Name="panel" Margin="10" RenderTransformOrigin="0.5,0.5">
        <TextBox x:Name="str" FontSize="30"></TextBox>
        <!--<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid, AncestorLevel=1}, Path=Tag}" FontSize="30"></TextBlock>-->
        <TextBlock FontSize="30" Text="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=ActualWidth}"></TextBlock>
    </StackPanel>
</Window>

方法四,不推荐

<Window x:Class="BindingTest.MainWindow"
        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:local="clr-namespace:BindingTest" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" >

    <Window.Resources>
    </Window.Resources>

    <StackPanel x:Name="panel" Margin="10" RenderTransformOrigin="0.5,0.5">
        <TextBox x:Name="str" FontSize="30"></TextBox>
        <!--<TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Grid, AncestorLevel=1}, Path=Tag}" FontSize="30"></TextBlock>-->
        <TextBlock FontSize="30" Text="{Binding RelativeSource={RelativeSource 2}, Path=ActualWidth}"></TextBlock>
    </StackPanel>
</Window>

ListBoxItem是否被选中

<Window x:Class="BindingTest.MainWindow"
        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:local="clr-namespace:BindingTest" 
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" >

    <Window.Resources>
    </Window.Resources>

    <StackPanel x:Name="panel" Margin="10">
        <ListBox>
            <TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsSelected}"></TextBox>
            <TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsSelected}"></TextBox>
            <TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsSelected}"></TextBox>
            <TextBox Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsSelected}"></TextBox>
        </ListBox>
    </StackPanel>
</Window>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/118324.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

linux内的循环

格式 while 【 条件判断 】 do 语句体 done 上图 第一次代码&#xff0c;输入语句在外面&#xff0c;结果输入完&#xff08;非hello&#xff09;程序不断循环&#xff0c;没办法&#xff0c;ctrlc给程序终止了&#xff0c;然后把用户输入的语句放到了循环体里面…

vivo发布“蓝心千询”自然语言对话机器人

&#x1f989; AI新闻 &#x1f680; vivo发布“蓝心千询”自然语言对话机器人 摘要&#xff1a;vivo今日发布了“蓝心千询”自然语言对话机器人&#xff0c;基于蓝心大模型。蓝心千询可以进行知识信息的快速问答&#xff0c;文学创作、图片生成&#xff0c;甚至还能编写程序…

SAM 微调在医学上的尝试

1、2023下半年 1、 UNet与SAM结合的正确的道路SAMUS,一路SOTA没对手&#xff01; https://github.com/xianlin7/SAMUS 2、 本文提出 SonoSAM&#xff1a;一种用于分割超声图像上感兴趣对象的快速基础模型。 https://zhuanlan.zhihu.com/p/663988684 未开源 绿色是预测的&…

流媒体服务实现H5实时预览视频

目录 背景方案业务实践细节注意 待办 背景 客户aws服务磁盘存储告急&#xff0c;最高可扩容16T。排查如下&#xff1a;主要是视频文件存在大量复制使用的情况。例如发布节目时复制、预览时复制&#xff0c;这样上传一份视频后最大会有四份拷贝&#xff08;预览、普通发布、互动…

http中的Content-Type类型

浏览器的Content-Type 最近在做web端下载的时候需要给前端返回一个二进制的流&#xff0c;需要在请求头中设置一个 writer.Header().Set("Content-Type", "application/octet-stream")那么http中的Content-Type有具体有哪些呢&#xff1f;他们具体的使用场…

作用域,基本数据类型(常量const),转义字符,运算符

1.作用域 全局作用域&#xff1a;定义在所有花括号外的名字具有“全局作用域” 块作用域&#xff1a;在某个花括号内定义的名字具有“块作用域” 一般把具有全局作用域的变量叫做“全局变量”&#xff0c;具有块作用域的变量叫做“局部变量” 如果在嵌套作用域里出现重名&a…

Linux进程的优先级

Linux进程的优先级 &#x1f4df;作者主页&#xff1a;慢热的陕西人 &#x1f334;专栏链接&#xff1a;Linux &#x1f4e3;欢迎各位大佬&#x1f44d;点赞&#x1f525;关注&#x1f693;收藏&#xff0c;&#x1f349;留言 本博客主要内容讲解Linux中进程的优先级&#xff0…

视频集中存储/云存储EasyCVR启动后查询端口是否被占用出错,该如何解决?

安防视频监控/视频集中存储/云存储/磁盘阵列EasyCVR平台可拓展性强、视频能力灵活、部署轻快&#xff0c;可支持的主流标准协议有国标GB28181、RTSP/Onvif、RTMP等&#xff0c;以及支持厂家私有协议与SDK接入&#xff0c;包括海康Ehome、海大宇等设备的SDK等。平台既具备传统安…

LINQ to SQL系列三 使用DeferredLoadingEnabled,DataLoadOption指定加载选项

介绍linq to sql 的 DataContext类DeferredLoadingEnabled属性使用,以及DataLoadOptions限定加载相关表数据的LoadWith和AssociateWith方法。 本文中举例用到的数据模型如下: Student和Class之间是多对一关系,Student和Course之间是多对多关系。 DataContext的DeferredLo…

策略模式在数据接收和发送场景的应用

在本篇文章中&#xff0c;我们介绍了策略模式&#xff0c;并在数据接收和发送场景中使用了策略模式。 背景 在最近项目中&#xff0c;需要与外部系统进行数据交互&#xff0c;刚开始交互的系统较为单一&#xff0c;刚开始设计方案时打算使用了if else 进行判断&#xff1a; if(…

宝塔面板使用Supervisor进程守护插件,配置守护Mysql的操作教程。

本篇文章主要讲解&#xff0c;在宝塔面板中使用Supervisor进程守护插件&#xff0c;配置守护Mysql的操作教程。 作者&#xff1a;任聪聪 日期&#xff1a;2023年11月5日 一、安装守护进程插件 安装插件一、进程守护插件 安装说明&#xff1a;在软件商店中搜索“进程守护”&am…

elasticsearch下载和安装(linux)看这一篇就够了

配置java环境&#xff08;11版本以上&#xff09; 1.下载安装包 我是放在usr下的java里了 2.解压 tar -zxvf jdk-17_linux-x64_bin.tar.gz3.配置环境变量 vim /etc/profile在文件的最下面添加 JAVA_HOME/usr/java/jdk-17.0.9 #你自己的安装路径 JRE_HOME$JAVA_HOME/jre C…

项目实战:组件扫描实现(1)-扫描类路径所有文件

1、ComponentScan 组件扫描类 一下知识本人都是在Maven工程下总结的&#xff0c;所以目录结构会不一样这个类的作用是扫描所有的classes目录下的所有的字节码文件&#xff0c;找到相应的类&#xff0c;然后找到相应类上的注解 package com.csdn.mymvc.core; import java.io.Fi…

6.数据类型与运算符

目录 mysql数据类型 整型数据类型 浮点数类型和定点数类型 1、日期时间类型 1、YEAR 2、TIME 3、DATE类型 4、DATETIME 5、TIMESTAMP 2、字符串类型 1、CHAR 和 VARCHAR类型&#xff1a; 2、TEXT类型 3、ENUM类型 4、SET类型 二进制字符串类型 1、BIT类型 2、…

链表面试OJ题(1)

今天讲解两道链表OJ题目。 1.链表的中间节点 给你单链表的头结点 head &#xff0c;请你找出并返回链表的中间结点。 如果有两个中间结点&#xff0c;则返回第二个中间结点。 示例 输入&#xff1a;head [1,2,3,4,5] 输出&#xff1a;[3,4,5] 解释&#xff1a;链表只有一个…

iOS17.2正式版什么时候发布? 13大新功能细节抢先看

苹果已经发布了针对开发者的iOS 17.2 Beta测试版&#xff0c;而iOS 17.2正式版预计会在2023年12月发&#xff0c;新版本给iPhone带来不少新功能&#xff0c;下面小编就带大家抢先了解iOS 17.2即将带来的13个新功能亮点细节。 1.手记Journal App上线 全新「手记」 Journal App终…

访问控制、RBAC和ABAC模型

访问控制、RBAC和ABAC模型 访问控制 访问控制的目的是保护对象&#xff08;数据、服务、可执行应用该程序、网络设备或其他类型的信息技术&#xff09;不受未经授权的操作的影响。操作包括&#xff1a;发现、读取、创建、编辑、删除和执行等。 为实现访问控制&#xff0c; 计…

使用cpolar配合Plex搭建私人媒体站并实现远程访问

文章目录 1.前言2. Plex网站搭建2.1 Plex下载和安装2.2 Plex网页测试2.3 cpolar的安装和注册 3. 本地网页发布3.1 Cpolar云端设置3.2 Cpolar本地设置 4. 公网访问测试5. 结语 1.前言 用手机或者平板电脑看视频&#xff0c;已经算是生活中稀松平常的场景了&#xff0c;特别是各…

软件开发项目文档系列之十如何撰写测试用例

目录 1 概述1.1 编写目的1.2 定义1.3 使用范围1.4 参考资料1.5 术语定义 2 测试用例2.1 功能测试2.1.1 用户登录功能2.1.2 商品搜索功能 2.2 性能测试2.2.1 网站响应时间2.2.2 并发用户测试 附件&#xff1a; 测试用例撰写的要素和注意事项附件1 测试用例要素附件2 测试用例的注…

el-input输入校验插件(正则表达式)

使用方法&#xff1a;在main.js文件中注册插件然后直接在<el-input>加入‘v-插件名’ (1)在main.js文件&#xff1a; // 只能输入数字指令 import onlyNumber from /directive/only-number; Vue.use(onlyNumber); &#xff08;2&#xff09;在src/directive文件夹中 &a…