WPF绑定元素-1-代码模式的绑定

This commit is contained in:
Bunny 2025-01-09 15:04:34 +08:00
parent 54c64230d8
commit dfe7544cc5
8 changed files with 127 additions and 0 deletions

View File

@ -46,6 +46,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPF控件-17-日期控件",
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPF绑定元素-1-示例", "WPF绑定元素-1-示例\WPF绑定元素-1-示例.csproj", "{4AC0C9A3-D0F3-458C-AB1F-A71027447202}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPF绑定元素-1-示例", "WPF绑定元素-1-示例\WPF绑定元素-1-示例.csproj", "{4AC0C9A3-D0F3-458C-AB1F-A71027447202}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WPF绑定元素-1-代码模式的绑定", "WPF绑定元素-1-代码模式的绑定\WPF绑定元素-1-代码模式的绑定.csproj", "{715FDA75-3236-4011-B1DA-46BD3EAFBA37}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -144,5 +146,9 @@ Global
{4AC0C9A3-D0F3-458C-AB1F-A71027447202}.Debug|Any CPU.Build.0 = Debug|Any CPU {4AC0C9A3-D0F3-458C-AB1F-A71027447202}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4AC0C9A3-D0F3-458C-AB1F-A71027447202}.Release|Any CPU.ActiveCfg = Release|Any CPU {4AC0C9A3-D0F3-458C-AB1F-A71027447202}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4AC0C9A3-D0F3-458C-AB1F-A71027447202}.Release|Any CPU.Build.0 = Release|Any CPU {4AC0C9A3-D0F3-458C-AB1F-A71027447202}.Release|Any CPU.Build.0 = Release|Any CPU
{715FDA75-3236-4011-B1DA-46BD3EAFBA37}.Debug|Any CPU.ActiveCfg = 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.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
EndGlobal EndGlobal

View File

@ -0,0 +1,9 @@
<Application x:Class="WPF绑定元素_1_代码模式的绑定.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF绑定元素_1_代码模式的绑定"
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绑定元素_1_代码模式的绑定;
/// <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,27 @@
<Window x:Class="WPF绑定元素_1_代码模式的绑定.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绑定元素_1_代码模式的绑定" Height="450" Width="800">
<StackPanel>
<Slider Name="SliderFontSize" Minimum="1" Maximum="100" Value="40" TickFrequency="1" TickPlacement="TopLeft" />
<Label>
Binding 绑定元素 ElementName=SliderFontSize 指定元素名称,Path=Value 指定绑定的值
</Label>
<TextBlock Margin="10" Text="简单的文字" Name="TextBlockShow" />
<WrapPanel>
<Button Margin="5 0" Padding="15 5" Name="SetSmall" Click="SetSmall_OnClick">设置小号</Button>
<Button Margin="5 0" Padding="15 5" Name="SetNormal" Click="SetNormal_OnClick">设置默认</Button>
<Button Margin="5 0" Padding="15 5" Name="SetLarge" Click="SetLarge_OnClick">设置大号</Button>
<Label>不要直接设置在字体上会使用字面绑定代替元素绑定或者使用Mode为TwoWay</Label>
</WrapPanel>
<WrapPanel>
<Button Margin="5 0" Padding="15 5" Name="ClearBinding" Click="ClearBinding_OnClick">清除绑定</Button>
</WrapPanel>
</StackPanel>
</Window>

View File

@ -0,0 +1,45 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace WPF绑定元素_1_代码模式的绑定;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Console.WriteLine("使用代码模式的绑定");
var binding = new Binding
{
Source = SliderFontSize,
Path = new PropertyPath("Value"),
Mode = BindingMode.TwoWay
};
TextBlockShow.SetBinding(TextBlock.FontSizeProperty, binding);
}
private void SetLarge_OnClick(object sender, RoutedEventArgs e)
{
SliderFontSize.Value = 100;
}
private void SetNormal_OnClick(object sender, RoutedEventArgs e)
{
SliderFontSize.Value = 40;
}
private void SetSmall_OnClick(object sender, RoutedEventArgs e)
{
SliderFontSize.Value = 10;
}
private void ClearBinding_OnClick(object sender, RoutedEventArgs e)
{
BindingOperations.ClearAllBindings(TextBlockShow);
}
}

View File

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<RootNamespace>WPF绑定元素_1_代码模式的绑定</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