简体   繁体   English

带转换器的C#WPF绑定路径数据

[英]C# WPF Binding Path Data with Converter

i have a problem converting a string to a icon. 我在将字符串转换为图标时遇到问题。 The icon Geometry is in a ResourceDictionary. 图标Geometry在ResourceDictionary中。 The ValueConverter is not called (i tried to debug in the Convert Method of the Converter). 未调用ValueConverter(我尝试在Converter的Convert方法中进行调试)。 Here is my code: 这是我的代码:

xaml: xaml:

<Window.Resources>
    <local:StatusToPathDataConverter x:Key="PathConverter"/>
</Window.Resources>
<Grid>
    <Path Width="20"
        Height="20"
        Stretch="Uniform"
        Fill="Black" 
        Data="{Binding Path=Status,
             UpdateSourceTrigger=PropertyChanged, 
             Converter={StaticResource PathConverter}}"/>
</Grid>

cs: CS:

public partial class MainWindow :Window {
    public MainWindow() {
        InitializeComponent();
    }

    public string Status
    {
        get { return (string)GetValue(StatusProperty); }
        set { SetValue(StatusProperty, value); }
    }

    public static readonly DependencyProperty StatusProperty =
         DependencyProperty.Register("Status", typeof(string), typeof(MainWindow));


}
public class StatusToPathDataConverter :IValueConverter {
    private static ResourceDictionary iconDictionary;
    public ResourceDictionary IconDictionary
    {
        get
        {
            if(iconDictionary == null) {
                iconDictionary = new ResourceDictionary();
                iconDictionary.Source = new Uri("/WPFBindingTest;component/Resources/IconDictionary.xaml", UriKind.RelativeOrAbsolute);
            }
            return iconDictionary;
        }
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        var status = (string)value;
        if(statinStatus == null)
            return null;
        switch(status.ToLower()) {
            case "test":
                return IconDictionary["TestIcon"];
                // ...
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        throw new NotImplementedException();
    }
}

You're not binding to anything. 您没有绑定任何东西。 You need to tell the Binding to go to the Window to find the Status property. 您需要告诉BindingWindow找到Status属性。 The recommended way to do that is to use RelativeSource , as below: 推荐的方法是使用RelativeSource ,如下所示:

<Path 
    Width="20"
    Height="20"
    Stretch="Uniform"
    Fill="Black" 
    Data="{Binding Path=Status,
         RelativeSource={RelativeSource AncestorType=Window},
         Converter={StaticResource PathConverter}}"
         />

As @Clemens notes, UpdateSourceTrigger=PropertyChanged doesn't make any sense on this binding and should not be there. 如@Clemens所述, UpdateSourceTrigger=PropertyChanged在此绑定上没有任何意义,因此不应存在。 That attribute tells the Binding when it should update the binding's source property. 该属性告诉Binding何时应更新绑定的source属性。 The source property is Window.Status , in this case. 在这种情况下,源属性是Window.Status

However, the Path.Data property does not update the property it's bound to. 但是, Path.Data属性不会更新其绑定的属性。 A Path displays a Geometry ; Path显示Geometry ; it doesn't edit a Geometry . 它不会编辑Geometry UpdateSourceTrigger exists for control properties that update viewmodel properties, like TextBox.Text . 对于更新视图模型属性的控件属性(例如TextBox.Text存在UpdateSourceTrigger That's the most common use for UpdateSourceTrigger=PropertyChanged : By default TextBox.Text updates the source property when the TextBox loses focus, but sometimes you want it to update on each keystroke. 这是UpdateSourceTrigger=PropertyChanged的最常见用法:默认情况下,当TextBox失去焦点时, TextBox.Text更新source属性,但有时您希望它在每次击键时进行更新。

Set the DataContext of the window to itself for the binding to work and the Convert method of the converter to get called: 将窗口的DataContext设置为其自身,以便绑定起作用,并调用该转换器的Convert方法:

public MainWindow() {
    InitializeComponent();
    DataContext = this;
}

If the binding to the source property fails the converter will never be invoked. 如果到source属性的绑定失败,则将永远不会调用该转换器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM