我正在尝试使用以下代码将DataColumn Header绑定到DynamicResource.
<Window.Resources>
<sys:String x:Key="HeaderText">Header Text</sys:String>
</Window.Resources>
<Grid>
<tk:DataGrid>
<tk:DataGrid.Columns>
<tk:DataGridTextColumn Header="{DynamicResource HeaderText}" Width="100"/>
</tk:DataGrid.Columns>
</tk:DataGrid>
</Grid>
Run Code Online (Sandbox Code Playgroud)
但由于一些奇怪的原因,列标题仍为空.然而,StaticResource效果很好.你能帮我弄清楚如何将Header属性绑定到某些DynamicResource.
我使用markupextension在WPF应用程序中加载国际化字符串,如下所示:
<Button Content="{Translate MyText}"/>
Run Code Online (Sandbox Code Playgroud)
我的markupextension命名为"TranslateExtension",它从数据库中搜索键"MyText"的值.它是这样做的
ProvideValue(IServiceProvider serviceProvider)
Run Code Online (Sandbox Code Playgroud)
返回正确字符串的方法.一切都很好.
我的问题是,不再调用ProvideValue-Method,并且在语言更改时无法从数据库中提取新字符串.我现在需要一种方法使返回值"动态",使Button重新加载它的xaml并重新使用markupextension它是否会通过更改语言或其他任何东西时抛出的事件.如何让系统再次调用ProvideValue-Method?我试过像InvalidateVisual()InvalidateArrange()InvalidateMeasure()UpdateLayout()...
我希望我清楚自己.请随时询问有关您认为能够提供想法或解决方案的更多信息.谢谢
我想根据bool的状态更改WPF控件的颜色,在这种情况下是复选框的状态.只要我使用StaticResources,这个工作正常:
我的控制
<TextBox Name="WarnStatusBox" TextWrapping="Wrap" Style="{DynamicResource StatusTextBox}" Width="72" Height="50" Background="{Binding ElementName=WarnStatusSource, Path=IsChecked, Converter={StaticResource BoolToWarningConverter}, ConverterParameter={RelativeSource self}}">Status</TextBox>
Run Code Online (Sandbox Code Playgroud)
我的转换器:
[ValueConversion(typeof(bool), typeof(Brush))]
public class BoolToWarningConverter : IValueConverter
{
public FrameworkElement FrameElem = new FrameworkElement();
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
bool state = (bool)value;
try
{
if (state == true)
return (FrameElem.TryFindResource("WarningColor") as Brush);
else
return (Brushes.Transparent);
}
catch (ResourceReferenceKeyNotFoundException)
{
return new SolidColorBrush(Colors.LightGray);
}
}
public object ConvertBack(object value, Type targetType,
object parameter, CultureInfo culture)
{
return …Run Code Online (Sandbox Code Playgroud) 首先是我开始的代码:
<ribbon:RibbonMenuButton IsEnabled="{Binding ForegroundIsConfigurable}"
SmallImageSource="{Binding Source={StaticResource imageSource},
Path=Source,
UpdateSourceTrigger=OnPropertyChanged}">
Run Code Online (Sandbox Code Playgroud)
虽然这个绑定正在编译并运行良好,但我不满意的原因是imageSource运行时更改.
StaticResource标记扩展:通过查找对已定义资源的引用,为任何XAML属性属性提供值.该资源的查找行为类似于加载时查找,它将查找先前从当前XAML页面的标记以及其他应用程序源加载的资源,并将生成该资源值作为运行中的属性值时间对象.
由于imageSource在运行时期间值的变化,我不得不StaticResource改为DynamicResource.但该属性Source不是依赖属性,因此以下代码将引发运行时错误:
SmallImageSource="{Binding Source={DynamicResource imageSource},
Path=Source,
UpdateSourceTrigger=LostFocus}
Run Code Online (Sandbox Code Playgroud)
出于这个原因,我需要直接绑定动态资源SmallImageSource,这是一个依赖属性:
SmallImageSource="{DynamicResource imageSource}"
Run Code Online (Sandbox Code Playgroud)
这又会引发运行时错误,因为imageSource它的类型是Image.SmallImageSource期望值为type的类型ImageSource.
现在可以建议将数据上下文设置为我的动态资源并适当地绑定属性.如果我这样做,我会杀死IsEnabled另一个属性的绑定DataContext.
据我所知,MultiBinding也不是解决方案,因为这提供了一种机制来将属性绑定到多个源,但不提供针对不同上下文和源的绑定不同属性.
在考虑如何继续下去时,我想到幸运的是,我可以将ImageSource钻井平台变成一个IValueConverter.在我给定的数据上下文中,我RibbonMenuButton有一个具有适当值的字符串值,这实际上也是我的来源ImageSource.
无论如何,我仍然想知道如果我没有其他方法,即如果两个源都在不同的数据上下文中,我将如何解决问题.有什么我没看到的吗?如何通过覆盖DataContext和绑定动态资源的属性来确保不杀死其他绑定?
这imageSource与DrawingImage msdn页面上的XAML示例非常相似.
<Image x:Key="imageSource">
<Image.Source>
<DrawingImage>
...
Run Code Online (Sandbox Code Playgroud) 在这种情况下,我希望使用资源字典中声明的字符串作为Text属性绑定的一部分.仅绑定单个动态资源字符串不是问题:
<TextBlock Text="{DynamicResource keyToMyString}" />
Run Code Online (Sandbox Code Playgroud)
但是如果你需要在MultiBinding上使用StringFormat,你很快就会遇到问题,因为你需要插入动态文本或想要组合几个字符串.例如,如果我的MultiBinding看起来像这样:
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1} some more text">
<Binding Source="{x:Static Resources:Strings.string1}" />
<Binding Source="{x:Static Resources:Strings.string2}" />
</MultiBinding>
<TextBlock.Text>
Run Code Online (Sandbox Code Playgroud)
我可以注入string1,并string2从指定的资源文件到绑定的文本,也没有问题.但我找不到以同样的方式使用动态资源中的字符串的方法.(我正在使用此方法将公司和产品名称从合并的资源字典中注入文本中).
使用TextBlock,我可以通过使用TextBlock内容的几个Run项来避免这个问题(参考):
<TextBlock >
<Run Text="{DynamicResource CompanyName}" />
<Run Text="{DynamicResource ProductName}" />
<Run Text="{DynamicResource MajorVersion}" />
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
但是当需要将动态资源绑定到Window Title属性时,这没有任何帮助.无论如何使用现有的标记扩展(如x:Static等)(创造性的,必要时)来实现这一目标?或者我们是否必须编写自己的标记扩展来实现这一目标?
我的程序的主菜单采用的是ContextMenu组成MenuItems.在我的程序(使用资源字典)的定位,我设置DynamicResource为Header我的每一个MenuItems.奇怪的DynamicResource编译,但似乎并没有影响本地化过程中的任何变化(语言上Headers没有变化).
例子MenuItem:
//I'm not sure if the x:Name or the PlacementRectangle is interfering with anything...
<ContextMenu x:Name="MainContextMenu" PlacementRectangle="{Binding RelativeSource={RelativeSource Self}}">
<MenuItem Header="{DynamicResource open}" />
</ContextMenu>
Run Code Online (Sandbox Code Playgroud)
MenuItem控制的限制是什么?它应该合作DynamicResource吗?我的总体目标是本地化这些strings,我该怎么做?
该程序在WPF中.谢谢.
更新: 这是我的App.xaml文件中引用我的资源字典的方式:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Lang.en-US.xaml" />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<Application.Resources>
Run Code Online (Sandbox Code Playgroud)
更新2: 我的英语资源字典中的示例字符串:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String x:Key="open">Open</sys:String>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
更新3: 我如何将当前资源字典更改为西班牙语的示例函数:
private void spanishChange_Click(object sender, RoutedEventArgs e)
{
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(
(ResourceDictionary)Application.LoadComponent(new …Run Code Online (Sandbox Code Playgroud) 如果资源字典不可观察,那么DynamicResource引用如何工作?资源字典的添加/删除方法是否具有内部代码,以便"轮询"所有DynamicResource引用并在删除或添加它们时刷新它们?
我正在代码中创建触发器,我正在尝试将setter的值绑定到后面代码中创建的动态资源,这样我就可以更新资源,并且仍然可以更新setter的值.我就此而已
SolidColorBrush brush = Brushes.Red;
Resources.Add("NewBrush",brush);
Setter setter = new Setter();
setter.Property = Control.BackgroundProperty;
Run Code Online (Sandbox Code Playgroud)
但我不确定如何将setter的值绑定到创建的动态资源.我不能简单地在XAML中创建资源,因为资源需要动态创建.如何将Setter的值绑定到动态资源,因此更改资源将改变setter的值.
更多信息要澄清.这都是在代码后面完成的,因为一切都是动态生成的.触发器,设置器,格式化,控件都是基于XML结构创建的.
我目前正在开发一个项目,我使用Caliburn在View和ViewModel之间进行绑定.为了能够在运行时在语言之间切换,我有单独的资源文件,其中包含应用程序中使用的所有字符串.一些例如TextBlock文本绑定绑定到其中一个字符串资源,如下所示:
SampleView.xaml
<TextBlock Text={DynamicResource Foo.Bar.Baz} ... />
Language.en-US.xaml
<system:String x:Key="Foo.Bar.Baz">Im a string</system:String>
当我将应用程序的文化更改为其他语言时,对Foo.Bar.Baz 的动态绑定使字符串在运行时更新为新语言.大!
但是,应用程序中的某些Text属性绑定到带有Caliburn的ViewModel中的字符串,如下所示:
SampleView.xaml
<TextBlock Text={Binding SampleText} ... />
SampleViewModel.cs
public string SampleText { get; set; }
值的值SampleText设置为Language.en-US.xaml中的字符串资源,如下所示:
...
SampleText = Application.Current.FindResource("Foo.Bar.Baz") as string;
...
不幸的是,当我更改应用程序文化时,字符串SampleText不会更新.
问题是: 如何将SampleText设置为Language.en-US.xaml中的字符串资源,当我更改应用程序文化时,它将自动更新?
注意:通过对此StackOverflow问题的评论,我读到可以通过类似的bindnig:
SampleText = Application.Current.Resource["Foo.Bar.Baz"] as string;
但是,这对我不起作用.
我正在尝试在大型 WPF 应用程序中设置主题框架。目前我们找到的解决方案是为每个调色板创建单独的 .xaml 文件,如下所示:
<LightColors.xaml>
<Color x:Key="MainPanelColor">Aqua</Color>
<Color x:Key="MainItemColor">Orange</Color>
<SolidColorBrush x:Key="MainPanelBrush" Color="{StaticResource MainPanelColor}" />
<SolidColorBrush x:Key="MainItemBrush" Color="{StaticResource MainItemColor}" />
Run Code Online (Sandbox Code Playgroud)
然后 UI 会像这样引用这些项目:
<Textblock Foreground="{DynamicResource MainItemBrush}"/>
Run Code Online (Sandbox Code Playgroud)
在 C# 中,调色板在运行时会更改。该主题框架完成了允许在运行时更改主题的任务。
问题:我想在 UI 和颜色之间创建一个层,以便调色板颜色可以链接到整个 UI 中使用的大量颜色定义。我发现接近有效的唯一解决方案是添加如下文件:
<ColorDefinitions.xaml>
<DynamicResource x:Key="Textblock_SetupPage_Foreground" ResourceKey="MainItemBrush" />
<DynamicResource x:Key="SecondDefinition" ResourceKey="MainItemBrush" />
Run Code Online (Sandbox Code Playgroud)
并在 UI 中引用这个新资源,如下所示:
<Textblock Foreground="{StaticResource Textblock_SetupPage_Foreground}" />
Run Code Online (Sandbox Code Playgroud)
不过,这个解决方案并不能完全发挥作用。它只允许单个 UI 元素使用 DynamicResources 之一,例如“Textblock_SetupPage_Foreground”,并且将 Textblock 引用更改为 DynamicResource 会产生错误。我怎样才能完成这个任务?
dynamicresource ×10
wpf ×9
c# ×4
xaml ×4
binding ×1
caliburn ×1
data-binding ×1
datagrid ×1
localization ×1
markup ×1
menuitem ×1
multibinding ×1
setter ×1
themes ×1
wpftoolkit ×1