<Window x:Class="WpfDependency.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:WpfDependency"
mc:Ignorable="d" Background="Gainsboro"
Title="MainWindow" Height="450" Width="800">
<DockPanel>
<local:DependecyUserControl x:Name="Abc" Height="30" HeaderTitle="{Binding Path=STitle,Mode=TwoWay,RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
DockPanel.Dock="Top" Background="#FFAAE7A7"></local:DependecyUserControl>
<DockPanel>
<StackPanel>
<local:KButton Click="KButton_Click" Height="100" Width="100" Margin="0,10,0,0" Content="篮子" Stretch="Fill" ForeImage="/Image/篮子.png" Background="Gray" MouseOverBackColor="Brown"/>
<TextBlock x:Name="AX" Height="50" Text="{Binding STitle,Mode=TwoWay}" Background="#FFEAB7B7" />
</StackPanel>
</DockPanel>
</DockPanel>
</Window>
其中绑定
HeaderTitle="{Binding Path=STitle,Mode=TwoWay,RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
可以写在C# ,等效代码如下
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{ STitle = "dd";
DataContext = this;
InitializeComponent();
// this.Abc.DataContext = this;
//Binding x = new Binding("STitle");
//x.Source = this;
//x.Mode = BindingMode.TwoWay;
Abc.SetBinding(DependecyUserControl.HeaderTitleProperty, new Binding("STitle")
{
Source = this,
Mode = BindingMode.TwoWay
});
}
例题完整代码https://download.csdn.net/download/kmsmg/86739723
在ListBox 和 ListView 显示用户控件的Binding 例题
数据:sessionData
显示控件:SessionShow
数据集 Binding 方法
<UserControl x:Class="BMS_Tool.Views.Content"
xmlns:Views="clr-namespace:BMS_Tool.Views"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:prism="http://prismlibrary.com/"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Core="clr-namespace:BMS_Tool.Core;assembly=BMS_Tool.Core"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:viewmodels="clr-namespace:BMS_Tool.ViewModels"
d:DataContext="{d:DesignInstance Type=UserControl}"
mc:Ignorable="d"
Width="Auto"
Height="Auto"
d:Width="400"
d:Height="400"
>
<UserControl.Resources>
<ResourceDictionary>
<Views:SessionConverter x:Key="_SdToShow"/>
<DataTemplate x:Key="SListViewTemplate">
<Frame Content="{Binding Converter={StaticResource _SdToShow}}" />
</DataTemplate>
<DataTemplate DataType="{x:Type Core:sessionData}" x:Key="sessionDataTemplate" >
<Views:SessionShow sData ="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}}"/>
</DataTemplate>
</ResourceDictionary>
</UserControl.Resources>
<Grid Margin="2">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition Height="30" />
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ScrollViewer Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="3" Grid.RowSpan="2" VerticalScrollBarVisibility="Auto">
<StackPanel >
<Views:SessionShow x:Name="test" sData="{Binding Path=Session ,Mode=TwoWay ,RelativeSource={RelativeSource AncestorType={x:Type UserControl }}}" />
<ListView x:Name="Hu" ItemsSource="{Binding Path=SessionCollection ,Mode=TwoWay }"
ItemTemplate="{StaticResource sessionDataTemplate}" >
</ListView>
</StackPanel>
</ScrollViewer>
<StatusBar Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3">
<TextBlock Text = "{Binding Session }" />
<CheckBox Content="显示" />
<CheckBox Content="深色模式" />
<Button x:Name="BT" Content="{Binding Session}" Click="BT_Click"/>
</StatusBar>
<ScrollViewer Grid.Row="3" Grid.RowSpan="2" Grid.Column="0" Grid.ColumnSpan="3" >
<ListBox x:Name="H" ItemsSource="{Binding Path=SessionCollection ,Mode=TwoWay }"
ItemTemplate="{StaticResource SListViewTemplate}"
Height="Auto" VerticalAlignment="Stretch" HorizontalAlignment ="Stretch" >
</ListBox>
</ScrollViewer>
</Grid>
</UserControl>
1. 实现方法 1核心:用“转换器” 将 sessionData --> SessionShow
<Views:SessionConverter x:Key="_SdToShow"/>
<DataTemplate x:Key="SListViewTemplate">
<Frame Content="{Binding Converter={StaticResource _SdToShow}}" />
</DataTemplate>
1. 实现方法 2 核心: <Views:SessionShow sData ="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}}"/>
<DataTemplate DataType="{x:Type Core:sessionData}" x:Key="sessionDataTemplate" >
<Views:SessionShow sData ="{Binding DataContext, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}}"/>
</DataTemplate>
显示控件: SessionShow
/// <summary>
/// Interaction logic for SessionShow
/// </summary>
public partial class SessionShow : UserControl, INotifyPropertyChanged
{
public String TextString
{
get
{
if ((bool)IsHex) return sData.HexString;
else
return sData.AsciiString;
}
set
{
try
{
if ((bool)!IsHex)
{
OnPropertyChanged(nameof(TextString));
sData.AsciiString = value;
}
else
{
sData.HexString = value;
OnPropertyChanged(nameof(TextString));
}
}
catch (Exception)
{
throw new ArgumentException("数值格式不正确"); ;
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName = "")
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
static void onIsHexchange(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
SessionShow source = (SessionShow)sender;
source.checkBox.IsChecked = (bool?)args.NewValue;
source.OnPropertyChanged(nameof(TextString));
}
/// <summary>
/// The DependencyID for the Text property.
/// Default Value: ""
/// </summary>
public static readonly DependencyProperty sDataProperty =
DependencyProperty.Register(
nameof(sData), // Property name
typeof(sessionData), // Property type
typeof(SessionShow), // Property owner
new FrameworkPropertyMetadata( // Property metadata
sessionData.Empty, // default value
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback(OnsessionDataPropertyChanged)
));
static void OnsessionDataPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
SessionShow source = (SessionShow)sender;
source.sData = (sessionData)args.NewValue;
source.OnPropertyChanged(nameof(sData));
source.OnPropertyChanged(nameof(TextString));
}
/// <summary>
/// Contents of the TextBox.
/// </summary>
//[Localizability(LocalizationCategory.Text)]
public sessionData sData
{
get { return (sessionData)GetValue(sDataProperty); }
set
{
SetValue(sDataProperty, value);
}
}
}