6

わかりました、これは非常に初心者向けの質問です。私が尋ねるのはほとんど恥ずかしいことです...

XAML ファイルでクラスを参照したいと考えています。これは、DataGrid 列の適切な編集テンプレートを選択するための DataTemplateSelector です。

とにかく、クラスをコード ビハインドに記述し、ローカル名前空間を XAML の一番上に追加しましたが、XAML からクラスを参照しようとすると、クラスがローカル名前空間に存在しないことがわかります. 本当に単純なものが欠けているに違いありませんが、理解できません...

これが私のコードです。

XAML:

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tk="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:local="clr-namespace:CustomFields"
xmlns:col="clr-namespace:System.Collections;assembly=mscorlib"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="CustomFields.MainWindow"
x:Name="Window"
Title="Define Custom Fields"
Width="425" Height="400" MinWidth="425" MinHeight="400">

<Window.Resources>
    <ResourceDictionary>
        <local:RangeValuesEditTemplateSelector>
            blah blah blah...
        </local:RangeValuesEditTemplateSelector>
    </ResourceDictionary>
</Window.Resources>

C#:

namespace CustomFields
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();

            // Insert code required on object creation below this point.
        }
    }

    public class RangeValuesEditTemplateSelector : DataTemplateSelector
    {
        public RangeValuesEditTemplateSelector(){

            MessageBox.Show("hello");
        }
    }   
}

私が間違っていることはありますか?これは1-2-3のように単純であるべきだと思いました...

ありがとう!

4

2 に答える 2

3

OK...突然動作し始めました。ただ再構築する必要がありました。

于 2010-03-26T14:25:46.363 に答える
1

コードの代わりに xaml でデータ コンテキストを設定できるように、キーを追加できます。

   <local:RangeValuesEditTemplateSelector x:key="RVETS">

次に、たとえば外側のグリッドの DataContext を設定します。

   <Grid DataContext={Binding Source = {StaticResource RVETS}} //Something like this I think

次に、そのグリッド内のすべてを、コードの背後で設定したプロパティに直接バインドできます。これが役立つかどうかはわかりませんが、共有したいと思いました:)

于 2012-04-22T03:49:18.977 に答える