<Window x:Class="ActivityLogs.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:ActivityLogs="clr-namespace:ActivityLogs"
Title="MainWindow" Height="350" Width="900">
<Window.Resources>
<ActivityLogs:RightMarginSetter x:Key="RightMarginSetter"/>
</Window.Resources>
<Grid>
<DataGrid AutoGenerateColumns="False" Name="m_gridControl">
<DataGrid.Columns>
<DataGridTextColumn Header="Message" x:Name="messagetype" Width="10*"/>
<DataGridTextColumn x:Name="columnCategory" Width="50*">
<DataGridTextColumn.Header>
<Grid Width="{Binding ElementName=columnCategory, Path=ActualWidth, Converter={StaticResource RightMarginSetter}, ConverterParameter=20}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="90*"></ColumnDefinition>
<ColumnDefinition Width="30"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0">Category</TextBlock>
<ComboBox Grid.Column="1" Background="{Binding ElementName=columnCategory, Path=Header.BorderBrush.Color.ColorContext}"></ComboBox>
</Grid>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<DataGridTextColumn Header="Recorded Time" x:Name="columnDateTime" Width="10*"/>
<DataGridTextColumn Header="Error Code" x:Name="columnErrorCode" Width="10*"/>
<DataGridTextColumn Header="Tool" x:Name="columnToolId" Width="10*"/>
<DataGridTextColumn Header="User" x:Name="columnUserId" Width="10*"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
嗨,上面一个是我的XAML表达式的XAML代码。 我只是用C#(Code behind)完全重写了它。
我无法创建静态资源并将其与Width属性绑定,就像我在XAML中所做的那样。 这里我的静态资源是RightMarginSetter,这是我创建的一个继承IValueConverter的类
在重写C#
中的部分时,我遇到了问题<Grid Width="{Binding ElementName=columnCategory, Path=ActualWidth, Converter={StaticResource RightMarginSetter}, ConverterParameter=20}">
你能救我吗?
谢谢。
答案 0 :(得分:0)
您可以尝试这种方式将转换器添加到当前Window的Resources
:
//create converter instance
var converterInstance = new RightMarginSetter();
//add the converter to window resources
this.Resources.Add("RightMarginSetter", converterInstance);
然后你可以在绑定中使用该资源:
//set up binding to use converter in resources
Binding myBinding = new Binding("ActualWidth")
{
ElementName = "columnCategory",
Converter = (RightMarginSetter)this.Resources["RightMarginSetter"],
ConverterParameter = 20
};
myGrid.SetBinding(Grid.WidthProperty, myBinding);