我需要根据要绑定的对象中标识的单元系统确定运行时StringFormat
的某些TextBlocks
绑定.
我有一个具有依赖属性的转换器,我想绑定.Bound值用于确定转换过程.
public class UnitConverter : DependencyObject, IValueConverter
{
public static readonly DependencyProperty IsMetricProperty =
DependencyProperty.Register("IsMetric", typeof(bool), typeof(UnitConverter), new PropertyMetadata(true, ValueChanged));
private static void ValueChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
((UnitConverter)source).IsMetric = (bool)e.NewValue;
}
public bool IsMetric
{
get { return (bool)this.GetValue(IsMetricProperty); }
set { this.SetValue(IsMetricProperty, value); }
}
object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (IsMetric)
return string.Format("{0:0.0}", value);
else
return string.Format("{0:0.000}", value);
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo …
Run Code Online (Sandbox Code Playgroud) 我有一些容器类通过ReadOnlyCollection公开它们的集合.自定义方法提供给集合中的添加和删除,它还执行一些自定义逻辑.
例如:
public class Foo
{
List<Bar> _barList = new List<Bar>();
public ReadOnlyCollection<Bar> BarList
{
get { return _barList.AsReadOnly(); }
}
public void AddBar(Bar bar)
{
if (bar.Value > 10)
_barList.Add(bar);
else
MessageBox.Show("Cannot add to Foo. The value of Bar is too high");
}
public void RemoveBar(Bar bar)
{
_barList.Remove(bar);
// Foo.DoSomeOtherStuff();
}
}
public class Bar
{
public string Name { get; set; }
public int Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这一切都很好但是当我使用Xml Serializer序列化Foo时会抛出异常.
任何人都可以提供一个好方法来解决这个问题吗?
谢谢
如何在运行时更改WPF静态资源的值?
我有以下资源
<UserControl.Resources>
<sys:String x:Key="LengthFormat">#.# mm</sys:String>
<sys:String x:Key="AreaFormat">#.# mm²</sys:String>
<sys:String x:Key="InertiaFormat">#.# mm?</sys:String>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
一些文字块参考
<TextBlock Grid.Row="2" Grid.Column="1"
Text="{Binding Path=Breadth, StringFormat={StaticResource ResourceKey=LengthFormat}}" />
Run Code Online (Sandbox Code Playgroud)
然后根据要绑定到控件的对象,我想改变格式.我在控件中设置了如下属性:
public string LengthFormat
{
set
{
this.Resources["LengthFormat"] = value;
}
}
public string AreaFormat
{
set
{
this.Resources["AreaFormat"] = value;
}
}
public string InertiaFormat
{
set
{
this.Resources["InertiaFormat"] = value;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在绑定之前我设置每个字符串.
但它不起作用,有人建议whynot?
干杯
嗨,我不得不学习VB.net以获得一份以前曾经是C#的新工作.我刚刚遇到了VB.net的一个有趣的功能.我可以引用尚未实例化的第二个表单上的对象!
所以从Form1我可以在Form2上获得textbox1的text属性,如下所示
Dim txt As String = Form2.TextBox1.Text
Run Code Online (Sandbox Code Playgroud)
谁能解释一下这是如何工作的?是否所有表格都在程序开始时进行了实例化,然后在程序生命周期内切换了它们的可见性?