0

編集:コンバーターを実装しようとしたときのGettignXaml解析エラー

エラー:「System.Windows.Markup.StaticResourceHolder」に値を指定すると、例外がスローされました。

<DataTemplate>
                    <myClasses:RegistrationButton x:Name="RegistrationButton" HorizontalAlignment="Center" Height="71" Width="148" 
                                                  Margin="10,0,5,0" 
                                                  Style="{DynamicResource ButtonStyleRegistration}"
                                                  Click="RegistrationButton_Click"
                                                  Title="{Binding Title}"
                                                  Oorzaak="{Binding Oorzaak}"

                                                  DuurStilstand="{Binding DuurStilstand, 
                                                        Converter={StaticResource DateTimeConvertor},  
                                                        ConverterParameter=\{0:t\}}"

                                                  BeginStilstand="{Binding BeginStilstand}"
                                                  />
                </DataTemplate>

IValueConvertor:

    public class DateTimeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // Retrieve the format string and use it to format the value.
        string formatString = parameter as string;
        if (!string.IsNullOrEmpty(formatString))
        {
            return string.Format(culture, formatString, value);

        }
        // If the format string is null or empty, simply call ToString()
        // on the value.
        return value.ToString();
    }

    // No need to implement converting back on a one-way binding 
    public object ConvertBack(object value, Type targetType,
        object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

現在、バインディングを使用してすべてを正しく表示するStringDependencyプロパティがあります。ただし、このプロパティは代わりにDateTimeにする必要があります。私は計算のためにアプリケーション全体で文字列を使用していますが、これは代わりに1つのDateTime値で実行する必要があります。

これをDateTimeに変更しようとすると、バインディングエラーしか発生しません。プロパティがString型である前。

public static readonly DependencyProperty DuurStilstandProperty =
        DependencyProperty.Register("DuurStilstand", typeof(DateTime), typeof(RegistrationButton), new UIPropertyMetadata(""));

public DateTime DuurStilstand
    {
        get { return (DateTime)GetValue(DuurStilstandProperty); }
        set { SetValue(DuurStilstandProperty, value); }
    }

XAMLバインディング:

<TextBlock x:Name="tbDuurStilstand" TextWrapping="Wrap" 
                                   Text="{Binding DuurStilstand, UpdateSourceTrigger=PropertyChanged}" 
                                   HorizontalAlignment="Center"
                                   VerticalAlignment="Center" 
                                   Margin="7.5,5,0,0" Height="24.8266666666667"/>

<ListBox.ItemTemplate>
                <DataTemplate>
                    <myClasses:RegistrationButton x:Name="RegistrationButton" HorizontalAlignment="Center" Height="71" Width="148" 
                                                  Margin="10,0,5,0" 
                                                  Style="{DynamicResource ButtonStyleRegistration}"
                                                  Click="RegistrationButton_Click"
                                                  Title="{Binding Title}"
                                                  Oorzaak="{Binding Oorzaak}"
                                                  DuurStilstand="{Binding DuurStilstand}"
                                                  BeginStilstand="{Binding BeginStilstand}"
                                                  />
                </DataTemplate>
            </ListBox.ItemTemplate>

ここにエラーが表示されます(XamlParseException):

<myClasses:RegistrationButton x:Name="btnTestRegistration" Content="Test Registratie" HorizontalAlignment="Left" Margin="16.8,0,0,118.14" 
            VerticalAlignment="Bottom" Width="119.2" Height="30.2" FontSize="18.667" Click="btnTestRegistration_Click" Style="{DynamicResource ButtonStyleRegistration}" />

これをDateTimeに変更できないのはなぜですか。

テキストブロックには、現在取得しているDateTimeではなくStringが必要です。コンテンツがテキストブロックの.textプロパティに配置されたときに、DateTimeを文字列に変換することをどの程度正確に確認できますか?

よろしく、PeterP

4

4 に答える 4

2

この場合、私の意見では、バインドされたデータとの間でBinding Convertersを利用するのが最善の解決策です。

public class DateTimeConverter: IValueConverter
{
        public object Convert(object value, Type targetType, object parameter, CultureInfo 
        {

           DateTime dt = (DateTime)value;
           //convert back to string from dt 

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
          string dateTimeString = value as string; 

           //Convert to DateTime from dateTimeString 
        }
    }

in を定義し、App.xaml と言う

<DateTimeConverter x:Key="DateTimeConverter" />

プロパティにバインドします

<myClasses:RegistrationButton x:Name="RegistrationButton" HorizontalAlignment="Center" Height="71" Width="148" 
                                                  Margin="10,0,5,0" 
                                                  Style="{DynamicResource ButtonStyleRegistration}"
                                                  Click="RegistrationButton_Click"
                                                  Title="{Binding Title}"
                                                  Oorzaak="{Binding Oorzaak}"
                                                  DuurStilstand="{Binding DuurStilstand}"
                                                  BeginStilstand="{Binding BeginStilstand}"
                                                  Converter = {StaticResource DateTimeConverter }


                        />
于 2012-04-19T13:27:20.103 に答える
1

さて、(DateTime から文字列への) 変換について話しているときは、バインディングにIValueConverterを追加するだけです。次の MSDN 記事には、DateTime から文字列に変換する方法を示す例があります。

http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter(v=vs.95).aspx

于 2012-04-19T13:27:10.477 に答える
1

あなたの問題はここにあると思います:

public static readonly DependencyProperty DuurStilstandProperty =
        DependencyProperty.Register("DuurStilstand", typeof(DateTime), typeof(RegistrationButton), new UIPropertyMetadata(""));

DateTimeを使用できますが、WPFはDateTimeに提供される値を強制する必要があり、DateTimeに強制UIPropertyMetadata([defaultvalue])できない空の文字列です。`UIPropertyMetadata(DateTime.MinValue) のようなものを使用する必要があります。

また、IValueConverterバインディングに s を適用することをお勧めします。これにより、WPF に依存して文字列を強制することはありませんが、これは強制できない可能性が非常に高く、代わりにコンバーターにいくつかのロジックを用意して、適切に処理します。

于 2012-04-19T13:28:17.450 に答える
0

プロパティの定義で、デフォルト値を空の文字列に設定しています。明らかに、これは DateTime では無効です! プロパティのタイプと実際に互換性のあるデフォルト値を指定する必要があります。

これを試して:

public static readonly DependencyProperty DuurStilstandProperty =
    DependencyProperty.Register("DuurStilstand", typeof(DateTime), typeof(RegistrationButton), new UIPropertyMetadata(DateTime.MinValue));

MinValue は、デフォルトとして意味のあるもの (おそらく DateTime.Now) に置き換えることができます。

バインド時に日付を実際にフォーマットするには、他の人が示唆しているように、StringFormatまたはコンバーターを使用できます。

編集

コンバーターの使用に関する問題ごと。コンバーターのインスタンスを作成する必要があり、それを参照できます。たとえば、次のようにできます。

<Window.Resources>
    <local:DateTimeConverter x:Key="MyConverter"/>
</Window.Resources>

そして、次のように使用します。

DuurStilstand="{Binding DuurStilstand, 
      Converter={StaticResource MyConverter},  
      ConverterParameter=\{0:t\}}"
于 2012-04-19T13:26:13.380 に答える