包括来自不同程序集的自定义转换器
我正在尝试从不同的程序集实现自定义转换器,但它似乎被忽略了。我已经解决了这个问题,但看不到我的错误,所以也许一些 XAML 忍者可以帮忙。这是相关代码...
xmlns:converters="clr-namespace:Shared.Converters;assembly=Shared"
以及资源字典...
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Shared;component/Styles.xaml"/>
<ResourceDictionary>
<converters:SaveStatusConverter x:Key="saveStateConverter" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
这是整个转换器本身。
命名空间 Shared.Converters { 公共类 SaveStatusConverter :IValueConverter {
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool? buttonState = (bool?)value;
Uri resourceLocater = new Uri("/Shared;component/Styles.xaml", System.UriKind.Relative);
ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater);
if(buttonState == true)
return resourceDictionary["GreenDot"] as Style;
if (buttonState == false)
return resourceDictionary["RedDot"] as Style;
return resourceDictionary["GreyDot"] as Style;
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new System.NotImplementedException();
}
} 这
是我的实现...
<ContentControl Style="{Binding Path=SaveState, Converter={StaticResource saveStateConverter}}"/>
我知道样式有效...这不是问题,我认为转换器也很好,它必须与我尝试调用它的方式有关,尽管我可以没有看到问题...
提前致谢。
I am trying implement a custom converter from a different assembly but it seems to be getting ignored. I have beat this to death and can't see my error so maybe some XAML ninja could help out. Here is the relevant code...
xmlns:converters="clr-namespace:Shared.Converters;assembly=Shared"
And the resource dictionary...
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Shared;component/Styles.xaml"/>
<ResourceDictionary>
<converters:SaveStatusConverter x:Key="saveStateConverter" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Here is the entire converter itself.
namespace Shared.Converters
{
public class SaveStatusConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool? buttonState = (bool?)value;
Uri resourceLocater = new Uri("/Shared;component/Styles.xaml", System.UriKind.Relative);
ResourceDictionary resourceDictionary = (ResourceDictionary)Application.LoadComponent(resourceLocater);
if(buttonState == true)
return resourceDictionary["GreenDot"] as Style;
if (buttonState == false)
return resourceDictionary["RedDot"] as Style;
return resourceDictionary["GreyDot"] as Style;
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new System.NotImplementedException();
}
}
}
And here is my implementation...
<ContentControl Style="{Binding Path=SaveState, Converter={StaticResource saveStateConverter}}"/>
I know the styles work... that isn't an issue, I think the converter is fine too, it must have something to do with the way I am trying to call it although I can't see the issue...
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
呃,咕咕咕……我的默认状态名称“灰色”而不是“灰色”有一个拼写错误 - 不管怎样 - 这对于如何做到这一点来说并不是一个太糟糕的例子。 :)
Err, grumble grumble... I had a typo in the name of my default state "gray" not "grey" - Anyway - this is not a too bad example of how to do this. :)