WPFマークアップ拡張機能

.Net 3.0のリリースでは、ベースクラスを独自のメソッドでオーバーライドせずに補足する機会があります。 このテクノロジーは、 コード拡張メソッドと呼ばれます。
しかし、判明したように、ウィンドウとコンポーネントのXAMLマークアップは、同じシンプルで非常に柔軟な方法で拡張できます。

拡張機能を使用する簡単な例は、よく知られた構造によってすべてに与えることができます
< Button Content ="{Binding Path=Title}" />

* This source code was highlighted with Source Code Highlighter .

Bindingは言語の拡張にすぎません。
そして今、あなたは長いコード行を通してのみ自分自身にバインドできるとき、前述の設計の欠点を修正する具体的な例です
< Button Content ="{Binding Path=Height,
RelativeSource={RelativeSource Self}}"
/>


* This source code was highlighted with Source Code Highlighter .

独自のSelfBinding拡張機能を作成します。
1.プロジェクトにクラスを追加し、SelfBindingと呼びます
2. MarkupExtensionからこのクラスを継承します
3.タイプstringのPathプロパティを追加します
4.空のコンストラクターを作成する
5. 1つのパラメーター文字列パスを持つコンストラクターを作成します
public SelfBinding( string Path)
{
this .Path = Path
}


* This source code was highlighted with Source Code Highlighter .

6. ProvideValueメソッドを再定義します
public override object ProvideValue(IServiceProvider serviceProvider)
{
try
{

if ( string .IsNullOrEmpty(Path))
throw new ArgumentNullException( "Path" , "The Path can not be null" );

// ,
var providerValuetarget = (IProvideValueTarget)serviceProvider
.GetService( typeof (IProvideValueTarget));

// ,
DependencyObject _targetObject = (DependencyObject)providerValuetarget.TargetObject;

//
PropertyInfo _sourceProperty = _targetObject.GetType().GetProperty(Path);

//
return _sourceProperty.GetValue(_targetObject, null );

}
catch (Exception ex)
{
Debug.WriteLine(ex);
return null ;
}
}


* This source code was highlighted with Source Code Highlighter .


これで確認できます
< Window x:Class ="for7raid.wpfExtension.Window1"
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local ="clr-namespace:for7raid.wpfExtension"
Title ="Window1" Height ="300" Width ="300" >

< Grid >
< Button Content ="{local:SelfBinding Foreground}" />
</ Grid >
</ Window >


* This source code was highlighted with Source Code Highlighter .

すべてが判明したので、標準のバインディングで行われているように、コンバーターを使用する機能を追加する必要があります。
7. IValueConverter型のプロパティコンバーターを追加し、結果を返すときにそれを確認します
// ,
if (Converter != null )
return Converter. Convert ( value , _targetProperty.PropertyType, null ,
Thread.CurrentThread.CurrentCulture);
else
return value ;


* This source code was highlighted with Source Code Highlighter .


それだけです!

まず、Namespaseを標準の名前空間にインポートします。これにより、各拡張子の前に接頭辞を記述する必要がなくなります。 Assembly.csファイルに次の行を追加し、プロジェクトをビルドします
[assembly: XmlnsDefinition( "http://schemas.microsoft.com/winfx/2006/xaml/presentation" , "for7raid.wpfExtension" )]

* This source code was highlighted with Source Code Highlighter .


TODO:
1.トリガーを追加してプロパティを変更し、ライブバインディングが存在するようにします
2.次の形式でパスのタスクを実装することが可能です:Path = Foreground.Name

このようにして、いくつかの問題を解決できます。たとえば、多言語対応のサポートを構築したり、ユーザーの権限に応じてフォーム上の要素の表示を監視したマネージャーを構築したりします。

< Button Content ="{Title btnKey, Default=Hello button}" ,
Visability ="{AllowedBy All sp1 sp2 sp3}" />


* This source code was highlighted with Source Code Highlighter .


ソースコード

続きを読む: マークアップ拡張機能とXAML
WPF多言語マークアップ拡張機能

Source: https://habr.com/ru/post/J84263/


All Articles