WPF绑定元素-2-代码检索绑定

This commit is contained in:
Bunny 2025-01-09 15:23:52 +08:00
parent dfe7544cc5
commit fe9bd2343e
8 changed files with 106 additions and 0 deletions

View File

@ -48,6 +48,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPF绑定元素-1-示例",
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPF绑定元素-1-代码模式的绑定", "WPF绑定元素-1-代码模式的绑定\WPF绑定元素-1-代码模式的绑定.csproj", "{715FDA75-3236-4011-B1DA-46BD3EAFBA37}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPF绑定元素-1-代码模式的绑定", "WPF绑定元素-1-代码模式的绑定\WPF绑定元素-1-代码模式的绑定.csproj", "{715FDA75-3236-4011-B1DA-46BD3EAFBA37}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPF绑定元素-2-代码检索绑定", "WPF绑定元素-2-代码检索绑定\WPF绑定元素-2-代码检索绑定.csproj", "{5797E06A-32B3-4F16-BC46-D88C8E5A5F48}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -150,5 +152,9 @@ Global
{715FDA75-3236-4011-B1DA-46BD3EAFBA37}.Debug|Any CPU.Build.0 = Debug|Any CPU {715FDA75-3236-4011-B1DA-46BD3EAFBA37}.Debug|Any CPU.Build.0 = Debug|Any CPU
{715FDA75-3236-4011-B1DA-46BD3EAFBA37}.Release|Any CPU.ActiveCfg = Release|Any CPU {715FDA75-3236-4011-B1DA-46BD3EAFBA37}.Release|Any CPU.ActiveCfg = Release|Any CPU
{715FDA75-3236-4011-B1DA-46BD3EAFBA37}.Release|Any CPU.Build.0 = Release|Any CPU {715FDA75-3236-4011-B1DA-46BD3EAFBA37}.Release|Any CPU.Build.0 = Release|Any CPU
{5797E06A-32B3-4F16-BC46-D88C8E5A5F48}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5797E06A-32B3-4F16-BC46-D88C8E5A5F48}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5797E06A-32B3-4F16-BC46-D88C8E5A5F48}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5797E06A-32B3-4F16-BC46-D88C8E5A5F48}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@ -0,0 +1,9 @@
<Application x:Class="WPF绑定元素_2_代码检索绑定.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF绑定元素_2_代码检索绑定"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>

View File

@ -0,0 +1,12 @@
using System.Configuration;
using System.Data;
using System.Windows;
namespace WPF绑定元素_2_代码检索绑定;
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}

View File

@ -0,0 +1,10 @@
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]

View File

@ -0,0 +1,21 @@
<Window x:Class="WPF绑定元素_2_代码检索绑定.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"
mc:Ignorable="d"
Icon="public/003540AH4M72.jpg" Title="WPF绑定元素_2_代码检索绑定" Height="450" Width="800">
<StackPanel Margin="20" Cursor="No">
<Slider Name="TextBlockSlider" Value="10" Maximum="100" Minimum="1" SmallChange="2" LargeChange="5"
TickPlacement="BottomRight" TickFrequency="5" IsSelectionRangeEnabled="True" />
<TextBlock Margin="10" Text="简单的文字" Name="SampleTextBlock"
FontSize="{Binding ElementName=TextBlockSlider,Path=Value,Mode=TwoWay}" />
<WrapPanel>
<Button Margin="5 0" Padding="15 5" Click="ButtonBase_OnClick">获取内容</Button>
<Button Margin="5 0" Padding="15 5" Name="ExpressionButton" Click="ExpressionButton_OnClick">获取绑定参数</Button>
</WrapPanel>
</StackPanel>
</Window>

View File

@ -0,0 +1,30 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WPF绑定元素_2_代码检索绑定;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var binding = BindingOperations.GetBinding(SampleTextBlock, TextBlock.FontSizeProperty);
Console.WriteLine(binding);
}
private void ExpressionButton_OnClick(object sender, RoutedEventArgs e)
{
var bindingExpression = BindingOperations.GetBindingExpression(SampleTextBlock, TextBlock.FontSizeProperty);
var resolvedSource = (Slider)bindingExpression!.ResolvedSource;
var sourceFontSize = resolvedSource.FontSize;
Console.WriteLine(sourceFontSize);
}
}

View File

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<RootNamespace>WPF绑定元素_2_代码检索绑定</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<Resource Include="public\003540AH4M72.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
</ItemGroup>
</Project>

Binary file not shown.

After

Width:  |  Height:  |  Size: 97 KiB