0

私のユーザー インターフェイスでは、ユーザー コントロールの上にタイトルを配置したいことがあります。

将来のローカライズのためにこれらのタイトルを XAML で宣言したいので、データ コンテキストから除外したいと考えています。

データバインディングは、ユーザーコントロールのルートノードに設定されたプロパティからそれらを取得できますか?

問題を次のコード例に要約しました。

using System.Windows;

namespace WpfApplication12
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            this.Person = new Author { Name = "Guge" };

            this.DataContext = this;
        }

        public object Person { get; set; }
    }

    public class Author
    {
        public string Name { get; set; }
    }
}

と:

<Window x:Class="WpfApplication12.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication12"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <DataTemplate DataType="{x:Type local:Author}">
        <Border AutomationProperties.Name="Author" BorderThickness="1" BorderBrush="Black">
            <Label Content="{Binding Name}"/>
        </Border>
    </DataTemplate>
</Window.Resources>
<StackPanel>
    <Label x:Name="Position" Content="Author"/>
    <ContentControl x:Name="presentation" Content="{Binding Person}"/>
</StackPanel>

実際の問題は、「Position」ラベルの content プロパティでデータバインディングを使用して、DataTemplate の Border の AutomationProperties.Name プロパティから「Author」という単語を取得するにはどうすればよいですか?

4

2 に答える 2

0

データオブジェクトを経由するルートはどうですか。

public class Author
{
    public string Name { get; set; }
    public string TypeName { get; set; } // might be better in base class Person
}

と:

<Window.Resources>
    <DataTemplate DataType="{x:Type local:Author}">
        <Border AutomationProperties.Name="{Binding TypeName}" 
                BorderThickness="1" BorderBrush="Black">
            <Label Content="{Binding Name}"/>
        </Border>
    </DataTemplate>
</Window.Resources>
<StackPanel>
    <Label x:Name="Position" Content="{Binding ElementName=presentation, Path=DataContext.TypeName}"/>
    <ContentControl x:Name="presentation" Content="{Binding Person}"/>
</StackPanel>
于 2010-10-08T07:35:36.753 に答える
0

これまでの解決策は、TypeName の文字列プロパティをビューモデルに追加し、コード ビハインドで AutomationProperties.Name の内容をこれに入力することです。そして、次のバインディングを使用します。

<StackPanel>
    <Label x:Name="Position" Content="{Binding Person.TypeName}"/>
    <ContentControl x:Name="presentation" Content="{Binding Person}"/>
</StackPanel>

ただし、ViewModel を使用せずにこれを行うことはまだ可能であると考えています。データバインディングのスキルが向上したときに、この問題を再検討できることを願っています。

于 2010-10-23T12:47:34.017 に答える