3

ResourceDictionary で埋められた App.xaml のみで構成されるウィンドウレス アプリケーションがあります。コードビハインドからそのディクショナリのコントロールにアクセスするにはどうすればよいですか?

4

1 に答える 1

3

VisualTreeHelper を介してコントロールを取得する、名前を介してコントロールに直接アクセスするなど、すべてがうまくいかなかったさまざまな方法を試した後、解決策は驚くほど簡単です。

ResourceDictionary.xaml

<ResourceDictionary x:Class="My.Namespace"
                    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Button x:Key="myButtonName" />

</ResourceDictionary>

ResourceDictionary.xaml.cs:

// Example with a button control
Button myButton= this["myButtonName"] as Button;

if(myButton != null)
{
 // Do something
}
于 2014-10-17T08:12:07.397 に答える