1

以下のXAMLを使用したテストWPFウィンドウがあります。

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfApplication2="clr-namespace:WpfApplication2"
        xmlns:test="clr-namespace:WpfApplication2.Properties"
        DataContext="{Binding Path=TestClass}"
        Title="MainWindow" Height="350" Width="525" >

    <Window.Resources>
        <wpfApplication2:TestTypeConverter x:Key="TestConverter"/>
    </Window.Resources>

    <Grid>
        <Grid Visibility="{Binding TestProperty, Converter={StaticResource TestConverter}, ConverterParameter='nottest'}">
            <Label Content="Test Label"></Label>
        </Grid>
    </Grid>
</Window>

以下のテストタイプコンバータークラスがあります。

using System;
using System.Globalization;
using System.Windows.Data;

namespace WpfApplication2
{
    class TestTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var valueToTest = (string) value;
            var parameterToCheck = (string) parameter;

            return valueToTest == parameterToCheck ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }
}

TestClassは以下のとおりです。

using System.ComponentModel;
using System.Runtime.CompilerServices;
using WpfApplication2.Annotations;

namespace WpfApplication2
{
    public class TestClass : INotifyPropertyChanged
    {
        public TestClass()
        {
            TestProperty = "test";
        }

        private string _testProperty;
        public string TestProperty
        {
            get { return _testProperty; }
            set
            {
                if (_testProperty == value)
                {
                    return;
                }
                _testProperty = value;
                OnPropertyChanged();
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

可視性プロパティはタイプコンバーターの影響を受けておらず、Convertメソッド自体も呼び出されていません(ヒットしていないブレークポイントを配置しました)。

私は何が間違っているのですか?

ありがとう

4

3 に答える 3

2

あなたのバインディングは間違っているようです:

 Visibility="{Binding 'test', Converter={StaticResource TestConverter}, ConverterParameter='nottest'}"

次のように変更します。

 Visibility="{Binding test, Converter={StaticResource TestConverter}, ConverterParameter='nottest'}"

プロパティテストが存在しない場合、コンバータは呼び出されません

于 2013-02-26T14:49:53.897 に答える
1

コンバーターパラメーターのリソースを作成し(以下のスニペットに示すように)、リテラルの代わりにコンバーターパラメーターとしてリソースを使用します

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:wpfApplication2="clr-namespace:WpfApplication2"
        xmlns:test="clr-namespace:WpfApplication2.Properties"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        DataContext="{Binding Path=TestClass}"
        Title="MainWindow" Height="350" Width="525" >
    <Window.Resources>
        <wpfApplication2:TestTypeConverter x:Key="TestConverter"/>
<sys:String x:Key="converterParam">nottest</sys:String>
    </Window.Resources>

    <Grid>
        <Grid Visibility="{Binding TestProperty, Converter={StaticResource TestConverter}, ConverterParameter={StaticResource converterParam}">
            <Label Content="Test Label"></Label>
        </Grid>
    </Grid>
</Window>
于 2013-02-26T15:54:31.453 に答える
0

datacontextがなくても機能する場合は、プロパティの名前を完全に省略してください。

Visibility = "{Binding Converter = {StaticResource myTestConverter}}"

// Converterキーワードの後に​​Bindingキーワードが続く方法を確認し、Bindingキーワードの後に​​プロパティ名を指定しませんでした。

于 2013-09-13T13:54:31.737 に答える